2014-10-12 45 views
5

我有很多需要重定向到新位置的URL,但是在某些情況下,子頁面需要保持活動狀態,而不是重定向。例如:.htaccess重定向父級但不是子級?

/products會重定向到http://www.newsite.com/products

/products/category1會重定向到http://www.newsite/products/category1

/products/specialitem不會在所有重定向。

這可能與重定向或RedirectMatch?

做一個Redirect 301 /products http://www.newsite.com/products似乎影響任何指導所有子網頁

謝謝!

編輯:

使用waynethec的回答,我就能上手。但任何人都可以澄清爲什麼我的第一條規則下面的作品,但其他人不?

RedirectMatch 301 ^segment-one$ http://www.google.com/

RedirectMatch 301 ^segment-one/segment-two$ http://news.google.com/

RedirectMatch 301 ^segment-one/segment-two/segment-three$ http://cnn.com/

RedirectMatch 301 ^segment-one/segment-two/segment-three/foobar$ http://gbv.com/

(如果不工作,我的意思是,我仍然可以獲取到的網頁,而不是將被重定向。)

+0

歡迎mod_rewrite'的'世界第一))當然,你可以做到這一點與RedirectMatch,但如果要求增加多少? - !開關的RewriteRules ..'RedirectMatch 301^/產品(/ specialitem) http:// www.newsite.com/products'雖然沒有檢查。 – Cheery 2014-10-12 00:29:32

回答

2

您應該可以使用以下RedirectMatch規則:

RedirectMatch 301 ^/products$ http://www.newsite.com/products 

請注意,這隻會重定向/ products,/ products /或/products/pagename.extension的請求。

+0

這真的很有幫助,但如果我的網址中有更多細分受衆羣,該怎麼辦?例如: '/ category1/subcat1'重定向到'http:// newsite.com/products' '/ category1/subcat1/product1/details /'重定向到http://newsite.com/products/ somepage' '/ category1/subcat1/product2/details2 /'重定向到'http:// newsite。com/products/somepage2' '/ category1/subcat2/foo/bar /'重定向到'http:// newsite.com/products/lorem/ipsum' – clorentzen 2014-10-12 14:55:31

+0

也許這會更好地問我的後續問題:爲什麼下面的第一條規則適用於我,但後來的規則不適用於? 'RedirectMatch 301^segment-one $ http:// www.google.com /' 'RedirectMatch 301^segment-one/segment-two $ http:// news.google.com /' 'RedirectMatch 301 ^第一個/第二個/第三個/第三個$ http:// cnn.com /' 'RedirectMatch 301 ^第一個/第二個/第三個/ foobar $ http:// gbv.com /' – clorentzen 2014-10-12 18:01:18

+0

您提供的語法是有效的,應該按照您的預期工作。你有.htaccess中的其他規則嗎? – waynethec 2014-10-14 17:32:08

0

您可以使用RedirectMatch

RedirectMatch 301 ^/products(?!/specialitem)(.*)$ http://www.newsite.com/products$1 

這會後重定向/products,或任何東西,除了/products/specialitem/

0

如果您需要添加的條件,這也爲我工作。請注意,使用RewriteRule時,我必須消除^products之間的斜線。

RewriteCond [...whatever your conditions are...] 

# test with 302 first to avoid annoying caching issues 
# RewriteRule ^products$ http://www.newsite.com/products [R=302,NC,L] 

# but use 301 in production once you know it's working 
RewriteRule ^products$ http://www.newsite.com/products [R=301,NC,L] 
相關問題