2012-08-06 40 views
1

我想重定向我的网站从www到非www,但没有“主页”。.htaccess重定向没有主页

EG。 www.domain.com或www.domain.com/什么也不做,但www.domain.com/everything-else应该重定向到domain.com/everything-else

为什么下面的代码也会重定向到“主页面”?

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

回答

1

也许是因为你还允许在RewriteRule^(.*)$ URL请求。

试试这个:

RewriteRule ^(.+)$ http://%1/$1 [R=301,NC,L] 

.*接受任何charachter零次或多次,而.+会接受任何字符一次或多次。

+0

非常感谢,它的工作原理 – Gnato 2012-08-07 06:45:47

0

对此, http://localhost/ (index.php says $_SERVER['REQUEST_URI'] is /)

我认为,即使显示网站“REQUEST_URI”的根目录不能为空,也会是“/”。所以也许你应该尝试这样的事情;

 
RewriteEngine On 

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC] 
RewriteCond %{REQUEST_URI} !^/$ 
RewriteRule ^(.+)$ http://domain.com/$1 [R=301,L] 
0

Isn`t that working?

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC] 
RewriteCond %{REQUEST_URI} !^\/?$ 
RewriteRule ^(.*)$ http://%1/$1 [R=307,NC,L]