Removing trailing slash from ALL URLs in site

后端 未结 3 1202
情深已故
情深已故 2020-12-06 07:52

I\'m kind of new to the whole .htaccess thing and I\'ve been trying to modify it so that none of my links will have trailing slashes at the end of their respective URLs. My

相关标签:
3条回答
  • 2020-12-06 08:24

    The issue is not caused by .htaccess, but rather by a combination of wordpress permalinks and .htaccess.

    1. Login to your site and navigate to the permalinks, then if you aren't using the custom structure option, switch to it and make sure to not have a trailing slash at the end:

      /%category%/%postname%
      
    2. Then add this to your .htaccess file, above the

      RedirectMatch 301 ^(.*)/$ /$1
      

    That is better than using rewrite since it's a redirect and not a rewrite.

    If that still doesn't work then I recommend you install the yoast seo plugin and there is a setting in it to do this.

    0 讨论(0)
  • 2020-12-06 08:25

    You can add a RewriteRule to eliminate the trailing slash:

    RewriteRule ^(.*)/$ $1 [R=301,L]
    
    0 讨论(0)
  • 2020-12-06 08:32

    This works for me; removing all trailing slashes from all routes while emphasizing that REQUEST_URI starts with a slash (at least in .htaccess files):

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

    Just don't use %{REQUEST_URI} (.*)/$. Because in the root directory REQUEST_URI equals /, the leading slash, and it would be misinterpreted as a trailing slash.

    SOURCE: https://stackoverflow.com/a/27264788/2732184

    0 讨论(0)
提交回复
热议问题