Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
1  
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. –  TechZilla Feb 7 '13 at 17:40
add comment

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.

share|improve this answer
    
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. –  TechZilla Feb 7 '13 at 17:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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