Redirect all but one file in a directory via httpd.conf / htaccess

夙愿已清 提交于 2019-12-20 01:34:34

问题


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:

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.


回答1:


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:

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



回答2:


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

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]


来源:https://stackoverflow.com/questions/9814100/redirect-all-but-one-file-in-a-directory-via-httpd-conf-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!