I have read many articles and cannot get ALL to combine .htaccess Rewrites to work together. I either get direct loops, or one or more do not work at all.
To be clear, I am looking for the following 5, if necessary:
- Provide www at all urls
- Provide HTTPS for all versions of the site.
- Remove index.php from URL
- Delete all .php extension / Redirect to url without .php extension
- May be in tandem with the previous one: add a trailing slash
Some examples:
Here is the current .htaccess setting:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://www.example.com/$1/ [L,R=301]
</IfModule>
The aforementioned .htaccess is ONLY in my root folder and currently makes 3 of 5 necessary: changes to HTTPS, adds www and removes index.php. It does not remove any other .php file extensions and does not add a trailing slash.
Toddn source
share