htaccess - 重定向到www并捕获所有index.php

时间:2015-05-13 23:27:08

标签: .htaccess mod-rewrite redirect web

我是htaccess的新手,所以请提前道歉。 我试图将所有非www重定向到www。到现在为止还挺好。与此同时,我也试图抓住所有和干净的链接(" index.php"未显示)到index.php。

我尝试将以下几行合并,但我没有成功使htaccess正常工作:

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

它将所有非www重定向到www,并且不会显示" index.php"。但我也想保留完整的网址。

示例:

example.com/subfolder应链接到www.example.com/subfolder,但会显示index.php

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我(希望)通过反复试验来回答我自己的问题。

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]

当然使用RewriteEngine