Redirect everything but one file to 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

      

Currently, httpd.conf has the following, it also redirects:

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

      

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

Basically, I want all URLs starting with "old.com/a/b/" to navigate to the new URL, except that I want one URL to be ignored.

+3


source to share


2 answers


Using negative form in regex 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 a redirect, use the $ 1 backreference, as in:

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

      

+5


source


I know this was answered, but for people looking to use the 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]

      

+3


source







All Articles