Remove trailing slash using .htaccess in WordPress

ぐ巨炮叔叔 提交于 2019-11-27 22:31:53

Try this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]

    RewriteRule (.+)/$ $1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

If it doesn't work try this one:

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]

    RewriteRule (.+)/$ http://www.domain.com/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

It might be as simple as go to settings > permalinks in the wp admin and remove the trailing slash at the input box for custom structure

Here are the steps to remove trailing slashes site-wide. If you simply wish to remove the trailing slashes on posts only, skip to Step 2.

Important Things To Consider

  • In .htaccess, the code between lines # BEGIN WordPress & # END WordPress may get reset by WordPress. Avoid changing code between those lines.
  • Forcing removal of trailing slashes, causes a loop in example.com/wp-admin. You can avoid the issue by excluding directories RewriteCond %{REQUEST_FILENAME} !-d.

The following solution addresses those issues.

Step 1 - Update /.htaccess file

Add the following code before the # BEGIN WordPress line in your /.htaccess file. This redirects URLs with trailing slashes to URLs with no trailing slashes.

# Remove trailing slashes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

</IfModule>

Step 2 - Update Permalinks

As others have pointed out, you must also update your Permalinks (Settings -> Permalinks) to Custom Structure, and remove the trailing slash there. It removes the trailing slash on all your posts.

To solve this trailing slash in WordPress URL problem, log in to your website admin panel -> go to Setting Tab -> click on Permalinks under Settings. It will open a page something like the below screen:

Now, check to verify the setting you have opted for the website URL structure. Make sure that you are not leaving a trailing slash in the URL structure. https://blog.techblogsearch.com/2018/11/29/guide-to-remove-trailing-slash-from-wordpress-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!