2011-08-08 47 views
2

我不知道單詞「可選參數」是否正確描述了我的情況。這是我需要的。URL中的可選參數 - 重寫規則

我寫的URL重定向以下規則:

RewriteRule ^product/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ product/?sid=$2&pid=$3&title=$1&src=$4 [NC,L] 

基本上,這會像

http://localdomain.com/product/Golf-Bats/abc123/def456/stringy 

重新導向到像

http://localdomain.com/product/?sid=abc123&pid=def456&title=Golf-Bats&src=stringy 

我想要做的就是編寫一個規則,它接受附加/可選/可能無限數量的參數(//類型結構),但是仍然重定向到相同的網址。

這意味着以下URL S:

http://localdomain.com/product/Golf-Bats/abc123/def456/stringy 
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1 
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1/rand2 
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1/rand2/rand3 
etc. 

應該都只是指向URL

http://localdomain.com/product/?sid=abc123&pid=def456&title=Golf-Bats&src=stringy 

任何想法?

回答

3

使用此規則 - 它將使第六,進一步路徑段「可選」:

RewriteRule ^product/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)(/.*)?$ product/?sid=$2&pid=$3&title=$1&src=$4 [NC,L] 

此規則將處理所有這些URL一樣(會重定向到同一URL):

http://localdomain.com/product/Golf-Bats/abc123/def456/stringy 
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1 
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1/rand2 
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1/rand2/rand3 
  1. 我已經a-z圖案更換A-Za-z既然你已經有了[NC]標誌(忽略大小寫)。

  2. 請記住,這種一般的網址是不是從SEO角度好 - 我強烈建議使用<link rel="canonical" href="PROPER_URL" />指定正確的網址,以避免搜索引擎重複內容懲罰:

  3. 這 「可選」 的部分將會丟失/不被傳遞到新的URL - 的要求。

+0

嘿,謝謝了。這就像一個魅力! – Nibhrit

+0

有用,謝謝;) –