Removing trailing slash from ALL URLs in site

2019-01-15 03:30发布

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 website is filmblurb.org.

The code for the .htaccess where Wordpress begins and ends looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I would appreciate it if someone can lead me in the right direction on how to fix this. Thanks.

3条回答
甜甜的少女心
2楼-- · 2019-01-15 03:57

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.

查看更多
该账号已被封号
3楼-- · 2019-01-15 03:58

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

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-15 04:03

You can add a RewriteRule to eliminate the trailing slash:

RewriteRule ^(.*)/$ $1 [R=301,L]
查看更多
登录 后发表回答