[wd_asp elements=’search’ ratio=’100%’ id=1]

Slash canonicalization at the end of the URL – htaccess

13th April 2022

Admin WP

wordpress category

If you have a website, you should always use the forward slash at the end of all urls.
If for some reason you do not, then this code is very helpful.

To remove a slash at the end:

example: – https://www.codehaven.co.uk

RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /$ [NC]
RewriteRule ^(.*)(/)$ $ 1 [L,R =301]

To add a slash to the end

example: – https://www.codehaven.co.uk/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ $1/ [L,R=301]