Ozzy
Ozzy

Reputation: 10643

.htaccess www redirect doesnt work properly

I want to redirect my website from

www.example.com to example.com

I use the following code in a ht access file:

Options +FollowSymlinks

RewriteEngine on


RewriteCond %{REQUEST_URI} ^/[\w]+/www/(.*)$
RewriteCond %{DOCUMENT_ROOT}/applications/%{REQUEST_URI} -f
RewriteRule ([\w]+\/www\/(.*)) /applications/$1 [NC,L,E=STOP:1]
#RewriteRule .* - [NC,L]


# Internal ENVs are prefixed with REDIRECT_
RewriteCond %{ENV:REDIRECT_STOP} !1

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


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

It works fine except but not quite.

When it redirects, it does this:

www.example.com > example.com/index.php?/
www.example.com/page/ > example.com/index.php?/page/

Where is the index.php? part coming from and how do I get rid of it?

Upvotes: 1

Views: 58

Answers (1)

anubhava
anubhava

Reputation: 785246

Order of your rules is the problem. Keep redirect rules before internal rewrite ones:

Options +FollowSymlinks

RewriteEngine on

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

RewriteCond %{REQUEST_URI} ^/[\w]+/www/(.*)$
RewriteCond %{DOCUMENT_ROOT}/applications/%{REQUEST_URI} -f
RewriteRule ([\w]+\/www\/(.*)) /applications/$1 [NC,L,E=STOP:1]
#RewriteRule .* - [NC,L]

# Internal ENVs are prefixed with REDIRECT_
RewriteCond %{ENV:REDIRECT_STOP} !1

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

Upvotes: 1

Related Questions