2012-09-28 122 views
0

我試圖重定向任何URL的符合以下條件的:的.htaccess重定向使用正則表達式匹配的URL

old-domain.com/marketplace/businesses/anything-here/ads/ 
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too 
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/ 
etc... 

new-domain.com/deal/ 

這是我的.htaccess文件看起來像

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteBase/
    RewriteRule ^marketplace/businesses/([a-z0-9\-_]+)/ads/(.*) http://new-domain.com/deal/ [R=301,L] 
</IfModule> 

我試過了幾個變化,包括交換([a-z0-9\-_]+)([A-Za-z0-9_-\s\.]+)[a-zA-Z-_0-9]+)和其他組合,但我似乎無法得到它的工作。

另請注意,我在網址anything-here的位置意味着我希望能夠匹配幾乎所有可能存在的內容。

任何幫助,將不勝感激。

謝謝

+0

我的本地機器上的快速測試證實,你的正則表達式應該適用於你想要做的事情。我想知道是否有什麼東西不像反斜槓用來逃避第一個模式組中的短劃線。您是否試過[-a-z0-9_]或[^ /]? – flergl

回答

1

問題是我有一個物理目錄/marketplace/businesses/.htaccess文件,阻止它的工作。

我能夠將其添加到該.htaccess文件,它的工作就像一個魅力。

感謝大家誰看了,並採取了刺。

0

我認爲這可能工作。 (有沒有嘗試它雖然)

Options +FollowSymlinks 
RewriteEngine on 
RewriteRule ^marketplace/businesses/(.*) http://new-domain.com/deal/$1 [NC] 

這將轉化這個

old-domain.com/marketplace/businesses/anything-here/ads/ 
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too 
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/ 

這個

http://new-domain.com/deal/anything-here/ads/ 
http://new-domain.com/deal/anything-here/ads/anyhting-after-this-too 
http://new-domain.com/deal/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/ 

還是喜歡我,你要保留部分完好,試試這個

Options +FollowSymlinks 
RewriteEngine on 
RewriteRule ^marketplace/businesses/([a-zA-Z0-9\-_]+)/ads/([a-zA-Z0-9\-_]+) http://new-domain.com/deal/$1/$2 [NC] 

這將被翻譯成這

http://new-domain.com/deal/anything-here/anyhting-after-this-too 

但我不認爲其他兩個將匹配的正則表達式。

+0

1個原因,這將無法正常工作,因爲我需要保持old-domain.com/marketplace/businesses/anything-here/完好無損,因爲我有一個轉發這些PHP腳本。另外,我正在使用301重定向。 – bigmike7801