Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 825
Elias Avatar asked Dec 27 '22 03:12

Elias


2 Answers

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
like image 55
mo-getter Avatar answered Jan 14 '23 03:01

mo-getter


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]
like image 33
Olivier Pons Avatar answered Jan 14 '23 05:01

Olivier Pons