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
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.
.htaccess
, the code between lines # BEGIN WordPress
& # END WordPress
may get reset by WordPress. Avoid changing code between those lines. RewriteCond %{REQUEST_FILENAME} !-d
.The following solution addresses those issues.
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>
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With