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

问题描述 投票:0回答:2

我该如何重定向父目录而不是子目录?请参阅下面我的具体问题。感谢所有和任何帮助!

家长 -

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]
.htaccess mod-rewrite http-redirect
2个回答
8
投票

发生这种情况是因为您第一次重定向。

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/

0
投票

上面的例子对我来说不起作用,所以我提供我的解决方案:

#redirect root "page" /cat-freelances/ to /freelances/ but not children pages. 
RewriteCond %{REQUEST_URI} ^/cat-freelances/?$ [NC]
RewriteRule ^ /freelances/ [R=301,L]
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.