Revo Burisan Revo Burisan - 3 months ago 13
Apache Configuration Question

Double slash and end slash on file

I am using

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]


this code helps to remove double slashes. But now I have another problem that I cant solve about an hour..

How can I remove the end slash if its a "file"?

http://domain.com/ <-- good
http://domain.com/file.php/ <-- bad


is it possible to write all those codes in one line or more professional? I am still learning.

Thank you!

Answer

Right below the RewriteEngine On line, add:

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

to enforce a no-trailing-slash policy.

To enforce a trailing-slash policy:

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