2011-05-04 181 views
0

htaccess RewriteRule似乎是正确的,但无法正常工作

我想重定向到任何.php文件的所有流量到index.php。

例如:
domain.com/test.php
domain.com/test/test.php
..应该去domain.com/index.php。同样的:
www.domain.com/test.php
www.domain.com/test/test.php
..应该去www.domain.com/index.php。

我已经添加了以下规则:(.COM)

重写规则(/)$ 1/index.php文件

其根据网上正则表达式/(* PHP的。)?测试人员应该给我正确的结果,但是当我在实际的htaccess上使用它时,规则似乎被忽略,并且给出了一个404错误。

我在做什么错?

回答

0

RewriteRule与第一个参数中的主机不匹配。请尝试以下操作:

RewriteEngine on 

RewriteCond %{REQUEST_URI} index.php$ 
RewriteCond %{QUERY_STRING} arg=something 
RewriteRule \.php$ /index.php? [R] 

RewriteCond %{REQUEST_URI} !index\.php$ 
RewriteRule \.php$ /index.php? [R] 

希望有所帮助。

+0

感谢您的帮助,但遗憾的是它仍然无法正常工作,它只是带来了而不是做重定向到index.php的404错误。 – user737182 2011-05-04 04:37:17

+0

任何想法为何如此?或者如果有其他方法,我应该采取? – user737182 2011-05-04 04:46:55

+0

您的index.php文件是否执行任何类型的重定向,或者您的'.htaccess'中是否有其他规则?该规则应该工作,你有权访问主Apache配置打开'RewriteLog',否则你确认'mod_rewrite'被启用?另外,我添加了一个条件来避免我昨晚忘记的重定向循环以及检查'index.php'上的查询字符串参数的规则。 – clmarquart 2011-05-04 12:35:27

0

试试这个代码:

Options +FollowSymlinks -MultiViews 
RewriteEngine on 

# internally redirect all .php files to index.php 
RewriteCond %{REQUEST_URI} !^/+index\.php [NC] 
RewriteRule \.php$ /index.php? [L,NC] 

# remove all query string from /index.php 
RewriteCond %{QUERY_STRING} !^$ 
RewriteRule ^index\.php$ /index.php? [L,R] 
+0

嗨,谢谢你的建议,但不幸的是它只会返回一个404错误,而不是实际上重写URL去index.php。任何想法,为什么是这种情况?我也有一堆spambot块和IP块,它们也不起作用。他们可能有关系吗? – user737182 2011-05-04 05:07:11

+0

很可能是这种情况。我建议你看看你的access.log,看看你为什么加载这个URL。 – anubhava 2011-05-04 05:09:25

+0

您还可以从上面的答案中复制/粘贴代码。我做了一个小修改。 – anubhava 2011-05-04 05:24:14