Jawad A.
Jawad A.

Reputation: 73

Codeigniter remove index.php when redirected from non-www to www

I have added a force redirect from non-www to www on my codeigniter based web app.

After doing that it is working fine, but its showing an extra index.php in URL which I need to remove.

Here is my URL : http://www.testprofile.com (WORKING FINE)

Here is problem caused :

https://testprofile.com/result/184/1103511096450940jawadamin (non www site, it will automatically add www.)

opening the above URL redirects to www but also adds an additional index.php in URL which should be removed.

HERE IS THE CODE OF MY .HTACCESS

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

Please help me remove this annoying index.php

Thanks,

Upvotes: 1

Views: 778

Answers (1)

Tpojka
Tpojka

Reputation: 7111

You missed to escape dot in right hand side of rewrite condition line. It should be like

RewriteCond %{HTTP_HOST} ^testprofile\.com [NC]

or in your whole example

<IfModule mod_rewrite.c>
RewriteEngine On
#If in root of web server you can use next line or set it appropriate according to location on server
#RewriteBase /

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

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

If dot is not preserved by back slash character apache read it as wild card character. You can find some useful tips on this page.

Also, you can check useful .htaccess snippets here.

Upvotes: 2

Related Questions