.htaccess with or without slash
-
22-08-2019 - |
Question
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
Solution
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]
OTHER TIPS
simple way to do this :
RewriteEngine On
RewriteBase /
RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow