How can I force www, remove trailing slash, force non-ssl and remove the .php extension using .htaccess?
I've tried a lot of things but I'm getting 500 internal server error a lot.
Thanks.
How can I force www, remove trailing slash, force non-ssl and remove the .php extension using .htaccess?
I've tried a lot of things but I'm getting 500 internal server error a lot.
Thanks.
Force www:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Remove trailing slash:
RewriteRule ^(.*)/$ $1 [R=301,L]
Force HTTP:
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L]
About removing the php extension, i'm not completely sure, but you could try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ %{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ %{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
However, this may fail for directories. But since i'm no longer using apache, i can just guess and not test it.
So, a combined solution could look like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ %{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ %{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteRule ^(.*)/$ $1 [R=301,L]