Alex Rivera | Logout

.htaccess with or without slash

Asked 2009-02-12T08:31:06.753
20

What do I need to do to the following rewrite rule to make it so it works whether or not their is a slash at the end of the URL?

ie. http://mydomain.com/content/featured or http://mydomain.com/content/featured/

RewriteRule ^content/featured/ /content/today.html 
Edit
Report

1 Answer

35

Use the $ to mark the end of the string and the ? to mark the preceding expression to be repeated zero or one times:

RewriteRule ^content/featured/?$ content/today.html

But I recommend you to stick to one notation and correct misspelled:

# remove trailing slashes
RewriteRule (.*)/$ $1 [L,R=301]

# add trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ $0/ [L,R=301]
answered 2009-02-12T08:45:57.513

Your Answer