2013-01-17 38 views
1

我有這樣的動態規則:如何製作htaccess動態和靜態規則?

RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC] 

,我嘗試添加一些靜態的規則是這樣的:

RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools 

問題是,當我在地址欄中鍵入以下:

http://www.{samedomain}.com/construction/pools.html

然後阿帕奇[R edirects到:

http://www.{samedomain}.com/construction-services/pools?sub=construction&second=pools.html

我要的就是Apache重定向到:

http://www.{samedomain}.com/construction-services/pools 

有誰知道爲什麼嗎?

謝謝。

+0

做你的動態URL中'.html'永遠結束? –

+0

不,這些是這樣的:/建設/池 – human

+0

好的,它應該工作,因爲我已經在下面添加。可能需要'%{REQUEST_FILENAME}'而不是'%{REQUEST_URI}' –

回答

1

重定向按它們出現的順序進行處理,所以它應該能夠在RewriteRule之前放置靜態重定向。請勿在RewriteRule上獲取[L]國旗。

RewriteEngine On 
# Match the static redirect first 
RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools 

# Since your dynamic URLs don't end in .html, avoid those with RewriteCond 
RewriteCond %{REQUEST_URI} !\.html$ 
RewriteRule ^([^/]+)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC,L] 

或者你可以做到這一點,而不RewriteCond如果沒有你的動態網址有一個.

RewriteRule ^([^/]+)/([^.]+)$ /rewrite.php?sub=$1&second=$2 [NC,L] 
+0

完美運作。非常感謝你! – human

+0

我需要你在這裏:http://stackoverflow.com/questions/14379842/why-if-i-put-a-dash-in-a-rule-in-my-htaccess-doesnt-work – human

1

寫一個基於優先級的htaccess的規則(將其最後有共同的行爲規則)

在你的情況

RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools 
RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC]