Make exception to whole-site RedirectMatch rule

Any reason you don't want to use mod_rewrite?

You could mimic the functionality with this:

RewriteCond  %{REQUEST_URI}  !^/(somepath|someotherpath)
RewriteRule  (.*)                       $1                        [R=permanent]
RewriteRule  http://%{SERVER_NAME}(.*)  https://www.foobar.com$1  [L]

I hadn't realized Apache uses PCRE. Since it does, you can do this bit of voodoo to do what you want with mod_alias:

RedirectMatch permanent ^/?((?!(thisisfoo|thisisbar)).*) https://www.foobar.com/$1

where /thisisfoo and /thisisbar are exceptions to the redirect.


According to the documentation (at http://httpd.apache.org/docs/2.2/mod/mod_alias.html#order), Redirects are proccessed in the order they are encountered. So you would want to put your more restrictive rule first, and then have your catch-all rule at the end.


I landed here before I found correct answer for my situation, so I'm adding my solution for reference.

I needed to redirect everything except two folders, I'm calling them files1 and files2 in the example below:

RedirectMatch permanent "^(/(?!(files1/|files2/)).*)" https://www.example.com$1

Likewise word permanent can be replaced by 301 which is essentially the same ("permanent" in HTTP code), or with 302 (which means temporary redirect).