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

10,871

Solution 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

Solution 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]
Share:
10,871
Elias
Author by

Elias

Updated on July 13, 2022

Comments

  • Elias
    Elias almost 2 years

    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.