2013-06-26 102 views
0

我有以下重寫規則mod_rewrite的URL(在第二部分條件)

RewriteRule ^/product/([^/]*)/([^/]*)/([^/]*)/([^/]*)/((test_data|data_test)/)?([^/]*)$ /product/?query_one=$1&query_two=$2&query_three=$3&query_four=$4&file_path=$6/$7 

在最後file_path的價值,我結合$6/$7

如果$6沒有值,那麼file_path只會是$7?我不想/ ID $6是無。簡單地說,我想把條件放在這部分。

編輯

https://example.com/product/one/two/three/four/test_data/file.jar

TO

https://example.com/product/?query_one=one&query_two=two&query_three=three&query_four=four&file_path=test_data/file.jar

問題是這個網址

https://example.com/product/one/two/three/four/test_file.xml

請建議?

謝謝!

+0

請提供您的源和目標URL的一些例子。 – anubhava

+0

@anubhava問題用例子更新 – Ahsan

回答

0

看起來你只需要簡化一下你的匹配模式。以下工作對我來說:

RewriteRule ^/?product/([^/]*)/([^/]*)/([^/]*)/([^/]*)/(.*?)/?$ /product/?query_one=$1&query_two=$2&query_three=$3&query_four=$4&file_path=$5 [L,QSA,NC] 

或者如果你想確保DIR只有(test_data|data_test)然後使用:

RewriteRule ^/?product/([^/]*)/([^/]*)/([^/]*)/([^/]*)/((?:(?:test_data|data_test)/)?.*?)/?$ /product/?query_one=$1&query_two=$2&query_three=$3&query_four=$4&file_path=$5 [L,QSA,NC] 
+0

不,我想爲這兩種類型的URL使用這種模式。如果'$ 6'爲空,我不希望'/'在'file_type'中。 – Ahsan

+0

除非我親自嘗試,否則我絕不會發布解決方案:P換句話說,上述「是」適用於您的兩個網址。 – anubhava

+0

在這種情況下,任何東西都可以用'(。*?)',但我只想給出兩個選項'(test_data | data_test)/' – Ahsan