2014-12-01 38 views
0

我有以下問題。比方說,我有鏈接像.htaccess如何只匹配URL中index.php的第一次出現?

http://localhost/test/index.php/admin

我有以下的htaccess:

Options -MultiViews 
Options +FollowSymLinks 
Options -Indexes 
RewriteBase /test/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L] 
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC] 
RewriteRule (.*?)index\.php/*(.*) $1$2 [R=301,NE,L] 

它的作用是簡單地從URL中刪除index.php文件。然而,當我輸入網址這樣的:

http://localhost/test/abc/blabla/index.php

它繼續無限循環。我想要的是當URL中另一個index.php被滿足時什麼也不做。我怎樣才能做到這一點?謝謝!

+0

'重寫規則^ $'是要匹配** **一切的URL。所以當重定向發生到index.php/index.php時,你會匹配,重定向到index.php/index.php/index.php等等...你需要排除'^ index.php' – 2014-12-01 19:55:14

+0

@MarcB你能告訴我該怎麼做嗎? – 2014-12-02 17:42:11

回答

0

這種替換代碼:(。*)

Options +FollowSymLinks -MultiViews -Indexes 
RewriteEngine On 
RewriteBase /test/ 

RewriteCond %{QUERY_STRING} !(^|&)r(&|$) 
RewriteCond %{THE_REQUEST} /index\.php [NC] 
RewriteRule ^(.*?)index\.php(?:/(.*))?$ $1$2?r [R=302,NE,L,QSA] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^((?!index\.php/).+)$ index.php/$1 [L,NC] 
+0

此代碼不適用於我。它刪除了兩個index.php,這樣 'http:// localhost/test/index.php/admin/index.php'或者 'http:// localhost/test/admin/index.php' 都返回 'http:// localhost/test/admin /' 我只想刪除第一個index.php,其他人(如果有的話)應該保留。 – 2014-12-01 23:00:52

+0

噢好吧,這將是棘手。現在查看更新的代碼。它會添加一個小的查詢參數'r'來防止進一步的重定向。 – anubhava 2014-12-02 06:27:21

+0

看起來不錯,但是有另一種方式,沒有'r'?我想要的只是第二,第三等。'index.php'成爲URL的一部分。 – 2014-12-02 17:45:24