简体   繁体   中英

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

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.

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM