Alain
Alain

Reputation: 63

Regex - how to match everything except a particular pattern

I'm using Apache and I want to redirect all received request to the ssl virtual host.

So I have the following line in the regular http virtual host:

RedirectMatch (.*) https://www.mydomain.com$1

which basicaly replace $1 by everything.

It works perfectly. But now, I need to access a particular CGI that cannot be on the SSL virtual host. So I would like to redirect all request, except the following:

"http://www.mydomain.com/mycgi/cgi.php"

I have search on this forum and found some post concerning regex exclusion, but none is working. Any help would be greatly appreciated.

Thanks. Alain

Upvotes: 6

Views: 2235

Answers (3)

arpadf
arpadf

Reputation: 443

The previous answers are correct, but what if tomorrow there will be another exception? You'll get a fat, hard understand regex. Is better (easier to understand and maintain) to use an If directive expression with Apache internal variables.

<If "%{REQUEST_URI} !~ m#^/mycgi/cgi.php$# && \
     %{REQUEST_URI} !~ m#^/anothercgi/cgi.php$#">
    RedirectPermanent / https://%{HTTP_HOST}/%{REQUEST_URI}
</If>

Upvotes: 0

Trey Hunner
Trey Hunner

Reputation: 11814

Apache 2.2 and later has negative lookahead support in regular expressions. If you are using Apache 2.2 or later this should work:

RedirectMatch ^/(?!mycgi/cgi.php)(.*) https://www.mydomain.com/$1

Upvotes: 3

zdav
zdav

Reputation: 2748

I believe the RedirectMatch is a short-circuit sorta deal. What this means, is that if you put another RedirectMatch ahead of your match-all, only the first match will execute. so something like...

RedirectMatch (/mycgi/cgi.php) http://www.mydomain.com$1 
RedirectMatch (.*) https://www.mydomain.com$1 

Upvotes: 1

Related Questions