We have this RedirectMatch rule to move clients on the SSL enabled site while maintaining the path they're trying to access:

Redirectmatch permanent (.*) https://www.foobar.com$1

and it is working perfectly. Now we would like to add an exception for /somepath that should not be redirected. How can I do that?

With mod_rewrite I would add a rewriterule saying "don't rewrite and stop matching", but there isn't such mechanism with redirect. Also, if I trivially reverse the regexp like !(/somepath|/someotherpath) it would no longer get the $1 parameter.


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.

  • 3
    mod_rewrite is never the answer to whole-site problems. – adaptr Oct 23 '12 at 12:52
  • @adaptr: I agree 100%, eventually you get mounds of convoluted match rules, the whole thing becomes a mess. Plus mod_rewrite requires more resources, and executes before all mod_alias declarations. So mod_rewrite is extremely powerful, but flexibility always comes at a cost. Sometimes it's the best tool for the job, but it seems like everyone just copies/pastes mod_rewrite rules for everything. – J. M. Becker Feb 7 '13 at 17:40
  • @adaptr, So are you indicating that this answer is awful/not recommended? Or am I misunderstanding your comment. That's fine, either way. If the former, then I would really like insight on what your recommended/proposed solution is. Without an alternative here, I'm about to do something very much like this. But if there is a better way, I'd really like to know! DevOps and Server stuff is hard to figure out. Way More so than front/back end, IMHO. – SherylHohman Feb 8 '19 at 20:25
  • Also, @j-m-becker, maybe you could weigh in on this (my question above)? - Sounds like you're maybe ok, with the above solution, but maybe not? Really just trying to understand. – SherylHohman Feb 8 '19 at 20:26

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.

  • 1
    Which works great, assuming your narrowly scoped declaration is a redirect. In this particular question, it's not a redirect, it's an exception to the site-wide redirect. Still a great tip, just not an answer related to the OP. On the other hand, this would make a great comment. – J. M. Becker Feb 7 '13 at 17:37

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.