本地服务器中的htaccess重定向问题

时间:2015-05-09 17:54:37

标签: apache .htaccess mod-rewrite

当我尝试在本地服务器(WAMP)中运行我的项目时,它将重定向到www.localhost.com

当我输入localhost / MYPROJECTNAME时,它会重定向到www.localhost.com

请在下面找到代码:

RewriteEngine on
RewriteBase /MYPROJECTNAME
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

2 个答案:

答案 0 :(得分:0)

试试这个,它不应该重定向,如果它是localhost。

RewriteEngine on
RewriteBase /MYPROJECTNAME
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

答案 1 :(得分:0)

否定localhost,更重要的是更改规则的顺序:

RewriteEngine on
RewriteBase /MYPROJECTNAME/

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]