2016-01-11 78 views
0

我想使用一個重寫規則更改格式的URL重寫規則添加缺少的參數以URL路徑

http://server.com/srv/index.php/12345/somedynamichash/somedynamichash/somedynamichash

其中/srv/index.php/12345/是靜態的格式的URL:

http://server.com/srv/index.php/12345/XXXX/somedynamichash/somedynamichash/somedynamichash

當且僅當在請求中缺少XXXX

無論這個

RewriteRule "^/(srv/index\.php/\d+/)(?!XXXX(?:$|/))(.*)" "$1XXXX/$2" [R] 

也不該組合

RewriteRule ^/srv/index\.php/(\d+)/(XXXX/.*)$ srv/index.php/$1/$2 [L] 
RewriteRule ^/srv/index\.php/(\d+)/(.*)$ srv/index.php/$1/XXXX/$2 [L] 

工作。

回答

1

RewriteCond是解決方案。它將應用重寫規則之下只有在$ 1 XXXX不匹配:

RewriteEngine on 
RewriteCond "$1" "!^XXXX$" 
RewriteRule "^/srv/index.php/12345/([^/]+)/(.*)$" "http://localhost/srv/index.php/12345/XXXX/$1/$2" 
+0

它爲我工作。你重新啓動Apache嗎? – Fredi

+0

編號在命令中替換兩次出現的XXXX應該是正確的。 – Fredi

+0

我剛剛意識到您的第一個RewriteRule適合我。你是否啓用mod_rewrite? – Fredi

0

這個規則應該在站點根目錄的.htaccess或者Apache配置爲你工作:

RewriteEngine On 

RewriteRule ^/?(srv/index\.php/\d+)/(?!XXXX(?:/.*)?$)(.*)$ /$1/XXXX/$2 [L,NC,R=302,NE]