Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect all URLs except one path and all its subpaths without mod_rewrite?

I have Apache 2.4 in front of Tomcat 7 (altough I think just the former matters here). I need to redirect all the requests on a domain to the HTTPS port, but not those going to a specific folder:

(1) http://www.example.com/ -> https://www.example.com/
(2) http://www.example.com/products -> https://www.example.com/products
(3) http://www.example.com/... -> https://www.example.com/...

while

(4) http://www.example.com/services should pass straight 
(5) http://www.example.com/services/main should pass straight
(6) http://www.example.com/services/main/list?params should pass straight

How can I accomplish this without using mod_rewrite (as I found was told in other questions)?

I saw in the docs that redirects are handled with the Redirect and RedirectMatch directives.

So I can handle 1,2,3 with:

<VirtualHost *:80>
    ...
    ServerName www.example.com
    Redirect / https://www.example.com
</VirtualHost>

but I can't write a regular expression to handle the other three. I found in this answer that I can use negative lookarounds, but a regex like /(?!services).* will only match 4 (at least according to Regex Tester on

EDIT

I've specified that this can be accomplished without mod_rewrite.

like image 844
watery Avatar asked Mar 17 '23 23:03

watery


1 Answers

Use regex based redirect rule here:

<VirtualHost *:80>
    ...
    ServerName www.example.com
    RedirectMatch 301 ^/((?!services).*)$ https://www.example.com/$1
</VirtualHost>

(?!services) is a negative lookahead that will any URL except the ones that are starting with /services

like image 191
anubhava Avatar answered Mar 19 '23 12:03

anubhava