.htaccess www重定向无法正常工作

时间:2014-07-17 10:52:55

标签: apache .htaccess mod-rewrite redirect http-status-code-301

我想从

重定向我的网站
www.example.com to example.com

我在ht访问文件中使用以下代码:

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]

除了但不完全正常。

当它重定向时,它会这样做:

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

index.php?部分来自何处以及如何摆脱它?

1 个答案:

答案 0 :(得分:1)

您的规则顺序是问题所在。在内部重写规则之前保留重定向规则:

选项+ 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]