简体   繁体   中英

.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?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM