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
Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.
How to add a trailing slash to URLs. One of the easiest and most convenient way to add or enforce trailing slashes on URLs is by RewriteRule directive. You can write a single rule in your htaccess file that will apply to all URLs without a trailing slash.
A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.
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]
simple way to do this :
RewriteEngine On
RewriteBase /
RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With