问题
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
回答1:
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]
回答2:
simple way to do this :
RewriteEngine On
RewriteBase /
RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC]
来源:https://stackoverflow.com/questions/540493/htaccess-with-or-without-slash