8

I'm looking for a way to remove trailing slash for all WordPress URL's.

I found similar answers like this one but it doesn't work when there's WordPress .htaccess rules before.

Here is my current WordPress .htaccess:

# 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
Community
  • 1
  • 1

5 Answers5

37

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

Frankey
  • 3,679
  • 28
  • 25
11

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. enter image description here

Timofey Drozhzhin
  • 4,416
  • 3
  • 28
  • 31
  • 1
    Should be considered as an accepted answer because of the begin wordpress thing! Well done! – Tom Aug 29 '19 at 11:37
10

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>
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • 3
    You should NOT update anything between the #BEGIN Wordpress block in .htaccess! – Yashvit Jan 21 '19 at 09:53
  • 1
    @Yashvit Saying that without giving any viable solution is useless. – Olivier Pons Jan 24 '19 at 15:16
  • Permalinks? The other answer? Do I need to repeat? – Yashvit Jan 25 '19 at 17:49
  • The directive that removes the trailing slash needs to go _before_ the `# BEGIN WordPress` section. (As @Yashvit pointed out, you should not edit the section between the `# BEGIN/END WordPress` comment markers because WP itself tries to maintain this code block and any custom directives are likely going to get overwritten. This is WP specific. Ordinarily, you would put the directive exactly as posted, unless you need to access directories (unlikely) then you need an additional _condition_ to exclude directories.) – MrWhite Feb 02 '21 at 11:18
0

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

Thuan Ng
  • 1
  • 2
  • 2
0

If you don't want to touch .htaccess manually as it is sensitive. Do the following steps.

  1. Go to Settings -> Permalinks and Change the custom structure and remove the ending Slash (/)

enter image description here

  1. Go to tools -> Permalink manager and then Settings tab.

a. Under Settings tab click on Redirect Settings then Enable Trailing slashes redirect by clicking checkbox.

enter image description here

b. Under settings tab click on General settings then Choose Trailing slashes options from drop down

enter image description here

MD TAREK HOSSEN
  • 129
  • 2
  • 11