301重定向 - 重定向父级但不是子级

时间:2015-01-08 23:33:52

标签: .htaccess mod-rewrite redirect

我如何重新指导父目录而不是儿童?请参阅下面的具体问题。所有和任何帮助表示赞赏!

家长 -

redirect 301 /community/whats-happening http://www.stapletondenver.com/whats-happening/  
  • 重定向父目录

儿童 -

redirect 301 /community/whats-happening/celebrating-halloween-stapleton http://www.stapletondenver.com/whats-happening/celebrating-halloween-in-stapleton/  

我尝试过其他一些选项但没有成功。见下文:

RewriteRule  ^community/whats-happening/. /whats-happening/ [R=301,L]

RewriteRule ^community/whats-happening/(.*)$ /whats-happening/$1 [R=301,NC,L]

RewriteRule ^/?community/whats-happening/?$ http://www.stapletondenver.com/whats-happening/ [R=301]

RewriteRule ^community/whats-happening/(.*) http://stapletondenver.com/whats-happening/$1 [R=301,L]
RewriteRule ^community/whats-happening/(.*)/(.*)/? http://stapletondenver.com/whats-happening/$1/ [R=301,L]

1 个答案:

答案 0 :(得分:6)

由于您的第一次重定向,这种情况正在发生。 Redirect指令将所有子目录和文件重定向到,所以:

Redirect 301 /abcd http://domain.com/xyz

表示

  • /abcd/ - > http://domain.com/xyz/
  • /abcd/1234/ - > http://domain.com/xyz/1234/
  • /abcd/1234/z.html - > http://domain.com/xyz/1234/z.html

如果您只想让父目录重定向,请使用带有RedirectMatch结束字符的正则表达式和$

RedirectMatch 301 ^/community/whats-happening/?$ http://www.stapletondenver.com/whats-happening/  
RedirectMatch 301 ^/community/whats-happening/celebrating-halloween-stapleton/?$ http://www.stapletondenver.com/whats-happening/celebrating-halloween-in-stapleton/