Question

I currently have this in my .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

When I type in the domain name without the www prefix:

[WHAT I TYPE INTO URL BAR => WHAT IT ENDS UP BECOMING]

example.com => http://www.example.com/ == GOOD!

And if I type in:

www.example.com/register => http://www.example.com/register == GOOD!

But when I type in:

example.com/register => http://www.example.com/index.php/register == BAD!

How do I remove the index.php portion for all corner cases? (Specifically the last one...)

Was it helpful?

Solution

Try this code

# First add the www to URL
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# The remove the index.php from URL
RewriteCond $1 !^(index\.php|ref_doc|media|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

The ordering is important I guess

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top