Rewrite - remove www, extension, slash redirect problems

Hi,

I’m will never really understand mode_rewrite rules - thats voodoo to me :rolleyes:

Anyway the most part is working for what i’m trying to do.

So i remove first the www. Than i don’t want the php extension and that works.
But i have a problem with the slash and directories and that the browser is telling me that there is a similar file instead giving me a 404.

So the url needs to be accessible with and without slash:

mydomain/whats-up

and

mydomain/whats-up/

but without slash is the canonical and the one with slash needs to redirect to the other one.

If whats-up is a directory without an index.file i want the “403 forbidden”.

So this part works:

ErrorDocument 404 /404-not-found

Options -MultiViews
Options -Indexes

RewriteEngine On

# Remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\\.php$ /$1 [R=301,L]

# Add .php back.
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /$1.php [QSA,L]

And i know this rule.
But just removing the slash tells me in Firefox that the redirect is in a loop if i try to call a directory. Not removing it gives me the “Multiple Choices” in the browser if i call “whats-up/”.

RewriteRule ^(.*)/$ /$1 [R=301,L]

And is there a way to redirect also every html to php because old files where html (for google)?

Nobody knows how to make it happen to call a url with slash and without slash (like google recommends) but if its a directory to don’t remove the slash?


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

:slight_smile:

Thank you - your the man! It works :slight_smile: