Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my htaccess I have some rewrited urls like these:

RewriteRule ^first-page$ /subdir/page.php?id_page=1 [QSA,L]
RewriteRule ^second-page$ /subdir/page.php?id_page=2 [QSA,L]
RewriteRule ^third-page$ /subdir/page.php?id_page=161 [QSA,L]

So the final result is: www.mywebsite.com/subdir/first-page

Now I would like to turn this into www.mywebsite.com/subdir/first-page/

Without the www.mywebsite.com/subdir/first-page giving a 404 error.

I tried this code after the url rewrites, but did not work:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]

Any suggestions? Thank you so much.

share|improve this question

migrated from superuser.com Dec 20 '12 at 16:05

This question came from our site for computer enthusiasts and power users.

up vote 4 down vote accepted

Try either

RewriteRule ^first-page/$ /subdir/page.php?id_page=1 [QSA,L]
RewriteRule ^second-page/$ /subdir/page.php?id_page=2 [QSA,L]
RewriteRule ^third-page/$ /subdir/page.php?id_page=161 [QSA,L]


RewriteRule ^first-page /subdir/page.php?id_page=1 [QSA,L]
RewriteRule ^second-page /subdir/page.php?id_page=2 [QSA,L]
RewriteRule ^third-page /subdir/page.php?id_page=161 [QSA,L]

The $ character tells Apache that this is the end of string and no more characters are expected. So you'll have to notify it to accept the slash at the end of the string, or to just be ready for any extra characters. You can use the ? flag to make the trailing slash not required, but accepted if present, e.g.:

RewriteRule ^first-page(/)?$ /subdir/page.php?id_page=1 [QSA,L]

So this won't let any other characters other than slash.

Hope this helps.

share|improve this answer
Thanks man! Adding (/)? at the end of the url solved the problem. However the preferred url is still the one without the slash. Could you please be so kind to tell me what could I do in order to make the URL with the slash as preferred? – F G Dec 20 '12 at 14:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.