Question

How can I achieve all of this using htaccess. Thus far I have -

To enforce SSL

RewriteCond %{HTTPS} !on [OR]

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]

RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]

And To remove index.php

RewriteCond %{THE_REQUEST} ^.*/index.php

RewriteRule ^(.*)index.php$ https://www.example.com/$1 [R=301,L]

it seems to work for the most part but when i enter something like - "example.com/index.php", i get an error. It is redirected to

https://www.example.com/https://www.example.com/

I know why its happening but can't figure out a rule to combine everything I need and make it work.

Was it helpful?

Solution

Going to take a guess, you're missing the L flag in the first rule:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Without the L flag, the request gets marked as "redirect" but continues on to have the other rules processed. And by the result you're seeing, it looks like the 2 rules are interferring with each other.


But is there any way to reduce the redirects when I type example.com/index.php

Not sure if this will work, because I can't test it, but maybe something like this?

RewriteCond %{THE_REQUEST} ^.*/index.php [OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*?)(index\.php)?$ https://www.example.com/$1 [R=301,L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top