1

I wish to remove a trailing slash when one is given using htaccess. What would be the best way to do it that will work with my existing rules as below:

  # make sure www. is always there
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  
  # if requested url does not exist pass it as path info to index.php
  RewriteRule ^$ index.php?/ [QSA,L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*) index.php?/$1 [QSA,L]

A sample URL would be something like:

https://www.example.com/this-test/

I of course want the ending slash removed.

I've tried this:

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

But that does not work with the existing rules that are there. It ends up redirecting to the index.php pages due to the other rules.

5
  • 1
    google.com/amp/s/helponnet.com/2020/02/20/… Commented Nov 11, 2021 at 17:48
  • 1
    Thanks for sharing your efforts in your question, please do mention which samples url you are hitting in your question. Commented Nov 11, 2021 at 17:48
  • Added more info. Commented Nov 11, 2021 at 18:06
  • Where in your existing rules did you try to insert your new rule? Commented Nov 11, 2021 at 18:22
  • I've tried in any position, can't get it to work. Commented Nov 11, 2021 at 18:28

1 Answer 1

2

Have it this way:

Options -MultiViews
DirectoryIndex index.php
RewriteEngine On

# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]

# if requested url does not exist pass it as path info to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [QSA,L]

Make sure to test it after completely clearing browser cache.

Sign up to request clarification or add additional context in comments.

7 Comments

Interesting. It's VERY close, but oddly if the first "folder" has a hyphen in the name, it ends up going to the last rule and passing the path to index.php instead?
There is no hyphen check in RewriteCond %{REQUEST_URI} ^(.+)/+$ besides by using RewriteCond %{REQUEST_FILENAME} !-d we are skipping all real folders. Real folders should always have a trailing / due to security reasons.
Well, with this rule "RewriteRule ^support-us/?$ /index.php?module=support_us [N]" and your suggestion it ends up as "/support-us?module=support_us" instead of "/support-us".
That rule is not part of my answer. Is /support-us/ a real directory?
You need to edit your question to include ALL your rewrite rules.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.