I am trying to make search engine friendly pages with .htaccess.

The page look like /en/hotel.php?id=nice-hotel-7-patterson-street and I would like the page to look like this /en/nice-hotel-7-patterson-street or /en/hotel-nice-hotel-7-patterson-street.

I am using this in my .htaccess file at the moment:

RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$          hotel.php?id=$2 [L]

but it does not seem to work in the /en/ folder and does not seem to work if letters are in the variable.


Your expression is incorrect. Since / is missing from the first part of the expression, it will not match anything which is in a sub-directory. Since the last expression requires there to be numbers at the end, it would work with URL that end in letters as per your examples.

You must generalize your expression, something like this:

RewriteRule ^en/([a-zA-Z0-9_-]+)$ hotel.php?id=$1

Now if you have multiple directories, you have to match that and pass it on somewhere different. If you have one hotel.php file in each directory use the first line below, if you have only one in all, use the second line where the directory is passed as another parameter, so that you can output different contents.

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1/hotel.php?id=$2

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ hotel.php?id=$2&lang=$1
  • Thank you Itai, the code you provided RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1/hotel.php?id=$2 seems to work, I was wondering if it would be possible to display the page with a .htm extension such as /en/nice-hotel-7-patterson-street.htm or /en/hotel-nice-hotel-7-patterson-street.htm
    – user29044
    Jun 24 '13 at 11:00
  • @user29044 - Just at \.htm before the $ in the rule. Also please remember to accept the answer if it solves your problem.
    – Itai
    Jun 24 '13 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.