Jethro Hazelhurst
Jethro Hazelhurst

Reputation: 3285

How to remove index.php from www to non-www .htaccess redirects

When I go to

www.example.co/courses

I get redirected to

https://example.co/index.php/courses

I want to get redirected to

https://example.co/courses

What I have tried

Here is my .htaccess file at the moment:

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

How do I remove the /index.php/ part of the redirected URL?

Upvotes: 1

Views: 444

Answers (1)

MrWhite
MrWhite

Reputation: 45914

You've got your external redirects in the wrong place. They need to go before your internal rewrites (before your front controller). For example:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

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

If you have your external redirects after the internal rewrite then the request gets rewritten to /index.php/... which then gets turned into an external redirect (the rewrite process loops in .htaccess, so it only gets rewritten to index.php on the first loop).

Upvotes: 1

Related Questions