apache.htaccesshttp-redirectmod-rewritetrailing-slash

Redirect URLs with a trailing slash to URLs with no trailing slash via htaccess rule with [QSA,L]


Recently I broke ties with WordPress and migrated all of my site's content to my own custom-made CMS. All works great except for one thing. All previous links to my site's blog posts have a trailing slash. Since none of my current URLs have a trailing slash, the previous links no longer work and my SEO is nearly non-existent. I've been attempting to find an htaccess rule that will redirect all trailing slash URLs to URLs with no trailing slash, but as of now, nothing works.


Solution

  • Use this redirect rule as your very first rule to remove trailing slash:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ /$1 [NE,R=301,L]