2011-05-04 180 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