通过httpd.conf / htaccess重定向除一个文件之外的所有目录中的文件

4
我想要进行重定向,具体如下...
http://old.com/a/b/ -> http://new.com/y/z/
http://old.com/a/b/file.php -> http://new.com/y/z/
http://old.com/a/b/c/file.php -> http://new.com/y/z/
http://old.com/a/b/anything -> http://new.com/y/z/
http://old.com/a/b/EXCLUDE.php -> http://old.com/a/b/EXCLUDE.php

我目前在httpd.conf文件中配置了以下内容,它可以正确地进行重定向:

RedirectMatch 301 /a/b/(.*) http://new.com/y/z/

我不知道如何排除一个文件不被重定向。

基本上,我希望所有以“old.com/a/b/”开头的URL都指向一个新的URL,但是我想忽略一个单独的URL。


根据http://httpd.apache.org/docs/2.0/mod/mod_alias.html,您不需要(.*); RedirectMatch替换URL的开头,因此/a/b/下的所有内容都将被捕获。至于您的问题,您可以在现有规则之前添加'RedirectMatch 301 /a/b/EXCLUDE.php /a/b/EXCLUDE.php',这样如何?我不知道它是否有效,但值得一试。 - jimw
2个回答

7
使用正则表达式中的负向前瞻应该可以解决问题:
RedirectMatch 301 /a/b/(?!EXCLUDE.php) http://new.com/y/z/

如果您想要重定向保留其余路径,请使用正则表达式中的反向引用 $1,例如:
RedirectMatch 301 /a/b/(?!EXCLUDE.php) http://new.com/y/z/$1

1
在看到您的答案之前,我探索了使用重写规则实现相同结果的方法。对于任何可能遇到这个问题并想要使用重写规则解决方案的人,以下规则似乎有效:RewriteEngine on RewriteRule ^/a/b/EXCLUDE.php$ - [L] RewriteRule ^/a/b/(.*)$ http://new.com/y/z [R=301,L] - Elias

3

我知道这个问题已经有人回答了,但是对于想了解RewriteRule的人们,以下是一些相关内容:

http://old.com/a/b/ -> http://new.com/y/z/
http://old.com/a/b/file.php -> http://new.com/y/z/
http://old.com/a/b/c/file.php -> http://new.com/y/z/
http://old.com/a/b/anything -> http://new.com/y/z/
http://old.com/a/b/EXCLUDE.php -> http://old.com/a/b/EXCLUDE.php

这应该可以工作:
RewriteCond %{HTTP_HOST} ^old\.com [NC]
RewriteCond %{REQUEST_URI} !^(/a/b/EXCLUDE\.php) [NC]
RewriteRule /a/b/(.*) http://new.com/y/z$1 [QSA,NC,R=301]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接