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

时间:2021-04-19 16:57:13

I would like to redirect as such...

我想重定向......

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

I currently have the following in httpd.conf and it redirects correctly:

我目前在httpd.conf中有以下内容,它正确地重定向:

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

I don't know how to exclude one file from being redirected.

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

Basically I want all URL's starting with "old.com/a/b/" to go to a singe new URL, except I want a single URL to be ignored.

基本上我希望所有以“old.com/a/b/”开头的URL都转到一个新的URL,除了我想要忽略一个URL。

2 个解决方案

#1


6  

Using a negative lookahead in the regular expression should work:

在正则表达式中使用否定前瞻应该有效:

RedirectMatch 301 /a/b/(?!EXCLUDE.php) http://new.com/y/z/

If you want the rest of the path to carry over with the redirect, use the backreference $1 as in:

如果您希望路径的其余部分继续使用重定向,请使用后向引用$ 1,如下所示:

RedirectMatch 301 /a/b/(?!EXCLUDE.php) http://new.com/y/z/$1

#2


3  

I know it's been answered but for people who want some RewriteRule stuff:

我知道它已被回答,但对于想要一些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

This should work:

这应该工作:

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]

#1


6  

Using a negative lookahead in the regular expression should work:

在正则表达式中使用否定前瞻应该有效:

RedirectMatch 301 /a/b/(?!EXCLUDE.php) http://new.com/y/z/

If you want the rest of the path to carry over with the redirect, use the backreference $1 as in:

如果您希望路径的其余部分继续使用重定向,请使用后向引用$ 1,如下所示:

RedirectMatch 301 /a/b/(?!EXCLUDE.php) http://new.com/y/z/$1

#2


3  

I know it's been answered but for people who want some RewriteRule stuff:

我知道它已被回答,但对于想要一些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

This should work:

这应该工作:

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]