Is this htaccess code the best/proper way to:
- Send all http traffic to https
- Send any visits to index.php to the URL that doesn't show "index.php"
- Send all non-www traffic to www
This code works to do those things I mentioned, I just want to make sure it's the most proper/efficient way to do it and/or it's ordered correctly...
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://www.example.com/1ドル [R=301,L]
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ https://www.example.com/1ドル [r=301,nc]
1 Answer 1
Keep your code consistent. If you are using RewriteRule
, use the same capitalisation format in all declarations. Same for setting the flags (R
, L
, NC
etc.); or the variables and so on.
I personally prefer to give an extra newline between any new set of RewriteCond
segment, so that I would be able to easily identify groups later on. As well as, adding comments never hurt anybody.
Prefer to handle the requests/paths at as late stage as possible.
Lastly, if you have just one domain name to handle; you can merge the https and naked-domain rules in a single one. The final ruleset would be:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index\.php [NC]
RewriteRule ^(.*)index\.php$ https://www.example.com/1ドル [R=301,L]