Search code examples
regex.htaccessmod-rewrite

Exclude specific directory from htaccess redirectmatch


I am migrating my blog from a subdirectory to the root of my domain, but I am leaving the admin panel in its current location, as such I need to do a 301 redirect for anything which matches the following path, except the "admin/" subdirectory.

I have the core redirect working with this:

RedirectMatch 301 (^/oldblogpath/)(.*) http://www.example.com/$2

The path I want to exclude from the above is "/oldblogpath/admin/" - please help me understand what I'm missing!


Solution

  • You can use a negative lookahead pattern for this:

    RedirectMatch 301 ^/oldblogpath/(?!admin/)(.*) http://www.example.com/$1
    

    (?!admin/) is a negative lookahead condition that fails the match of admin/ appears right after matching starting directory path.

    Make sure to clear old browser cache completely before testing this change.