Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

we have urls as;

 - mydomain.com/my-keyword/
 - mydomain.com/my-other-keyword/
 - mydomain.com/my-another-keyword/

and also

 - mydomain.com/my-keyword/a-page.html
 - mydomain.com/my-keyword/another-page.html
 - mydomain.com/my-other-keyword/some-page.html
 - mydomain.com/my-other-keyword/any-page.html
 - mydomain.com/my-another-keyword/another-page.html
 - mydomain.com/my-another-keyword/some-page.html

We want to convert (301) all urls created without / for example; mydomain.com/my-another-keyword -> to -> mydomain.com/my-another-keyword/ without harming the rest of the url structure.

Any ideas?

share|improve this question

migrated from stackoverflow.com Jul 4 '11 at 10:09

This question came from our site for professional and enthusiast programmers.

2 Answers 2

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1/ [L,R=301] 

if the file exists: dont add slash, if it's not a file: add slash

share|improve this answer
this would NOT work, because we dont have a files with those names. Basically our original rewrite can rewrite anything regardless of what address is called as long as keyword exists. It will pull out the details and print them on page. mydomain.com/colorado-hotels/ or mydomain.com/colorado_hotel/ They both bring out the same data. So obviously mydomain.com/colorado_hotel will also work. Thanks but, this is why yours is no good. Any ideas ? –  hasbehas Jul 8 '11 at 11:13

This should work for you as long as a "directory" in the path does not contain a .

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) http://domain.com/$1/ [R=301,L]
share|improve this answer

Your Answer


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

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