Vitalij
Vitalij

Reputation: 680

Force only 1 one slash at the end

I want on my wordpress force only one slash at the end of some post or page url.

When WP page is not cached, I am in admin, is all ok.

Situations like:

  1. somewww.com/somepage
  2. somewww.com/somepage//
  3. somewww.com/somepage///

all is forced to somewww.com/somepage/ where is only one slash.

But if we are on cached version of page, then urls are opening without forcing 1 slash at the end.

So, I've finded second code (on WP rocket website):

# Force trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|xml|txt|js|php|scss|webp|mp3|avi|wav|mp4|mov)$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]

In htaccess I writed code at begining.

So, currently working well only situation number 1, other failed.

Who can help me with page urls rewriting on Wordpress? Thanks

Upvotes: 0

Views: 37

Answers (1)

anubhava
anubhava

Reputation: 785611

You may use this rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_URI} !/$ [OR]
RewriteCond %{THE_REQUEST} \s[^?]*/{2,}[\s?]
RewriteRule ^(.*?)/?$ https://%{HTTP_HOST}/$1/ [L,R=301,NE]

Make sure to clear your browser cache before you test this rule.

Upvotes: 1

Related Questions