2

This is more like a follow up question to this one: .htaccess - how to force "www." in a generic way?

I have this rules:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

And I would like to merge them with the ones provided on that answer:

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

Are these the correct results (right order and rules)?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

So far everything works as expected but all my tests where done in a temporal URL and not on the actual domain.

Thanks!

Community
  • 1
  • 1
Moises Kirsch
  • 133
  • 1
  • 5

1 Answers1

1

Those rules should do what you're looking for, but I would tweak it slightly. Since what you have works, this is all just suggestions based on my preferences. :)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php/$1 [L]
</IfModule>

I find that it's easier to read when all of the Rewrite options that are not rules come first. I also like to add blank lines so it's easier to see where one set of rules ends and the next begins.

This rule is unnecessary: RewriteRule ^index\.php$ - [L] as the next rule will only impact requests for URIs that don't map to a real file. Because index.php exists on the filesystem, it will not trigger the next rule.

For the index.php rule, adding the /$1 makes the request available in $_SERVER['PATH_INFO'], which can be helpful depending on how your code is written. This is definitely not required, but it's pretty common for applications like WordPress or CodeIgniter to suggest this approach, so I thought it was worth mentioning.

bradym
  • 4,734
  • 27
  • 35