5

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.

Community
  • 1
  • 1
watery
  • 5,026
  • 9
  • 52
  • 92

1 Answers1

7

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

anubhava
  • 761,203
  • 64
  • 569
  • 643