Using RedirectMatch with HTTP_HOST in the destination
Asked Answered
A

3

10

I keep reading that, where possible, I should not be using mod_rewrite. As such, I am trying to do a http to https rewrite with RedirectMatch.

Question: How can I use RedirectMatch and use Apache server variables (such as %{HTTP_HOST}) in the URL parameter?

This code fails to return a response to the client (Chrome):

RedirectMatch ^(.*) https://%{HTTP_HOST}/$1

I recently asked a similar question to this, but it may have been too wordy and lacks direction for an answer: Redirecting http traffic to https in Apache without using mod_rewrite

Asco answered 27/10, 2016 at 15:58 Comment(0)
C
10

No, You can't use variables of that type with Redirect/RedirectMatch. If you need variables, such as %{HTTP_HOST}, use mod_rewrite.

Note: I commend you for not trying to use mod_rewrite right away, because most people will go for mod_rewrite even for the simplest of redirections, which is clearly overkill and most times it is just looking to complicate things unnecessarily.

Chichihaerh answered 27/10, 2016 at 16:12 Comment(1)
Thanks! This problem has been unproductively burning a hole in my head for a day now. On new implementations, I like to try and do things 'the right way' even if it's harder. In this case, mod_rewrite it is!Asco
A
16

If you're using 2.4.19 or later, the Redirect directive has a somewhat obscure feature: putting it inside a Location or LocationMatch will enable expression syntax.

So your example can be written as

<LocationMatch ^(?<PATH>.*)>
    Redirect "https://%{HTTP_HOST}%{env:MATCH_PATH}"
</LocationMatch>

(Here, the ?<PATH> notation means that the match capture will be saved to an environment variable with the name MATCH_PATH. That's how we can use it later in the Redirect.)

It's even easier if you always redirect using the entire request path, because you can replace the capture group entirely with the REQUEST_URI variable:

<Location "/">
    Redirect "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>

Now, is this easier to maintain/understand than just using mod_rewrite for this one case? Maybe not. But it's an option.

Arce answered 27/10, 2016 at 17:42 Comment(5)
The Apache config syntax isn't actually XML, so <Location /> just means that the argument to Location is "/". Surrounding it in quotes is probably more readable in this case.Arce
I'm already doing all this inside a LocationMatch so that would have been gorgeous. Alas, we are stuck, for now, on 2.4.6 as we are using RHEL7. All the good stuff came later lol. I may consider the RedHat Software Collections version (2.4.18) if there are enough compelling reasons to leave the version included in core RHEL7.Asco
FWIW Jacob had taught me about this example earlier, but imho I thought it was too convoluted to be considered a good answer.Chichihaerh
if you add the www in Redirect "https://www.%{HTTP_HOST}%{REQUEST_URI}" if you browse yoursite.com it will redirect to www.yoursite.comAmpersand
<Location "/"> can create an infinite redirect loop. To redirect your web root, use <Location ~ "^/$"> instead.Jasminjasmina
C
10

No, You can't use variables of that type with Redirect/RedirectMatch. If you need variables, such as %{HTTP_HOST}, use mod_rewrite.

Note: I commend you for not trying to use mod_rewrite right away, because most people will go for mod_rewrite even for the simplest of redirections, which is clearly overkill and most times it is just looking to complicate things unnecessarily.

Chichihaerh answered 27/10, 2016 at 16:12 Comment(1)
Thanks! This problem has been unproductively burning a hole in my head for a day now. On new implementations, I like to try and do things 'the right way' even if it's harder. In this case, mod_rewrite it is!Asco
Q
0

Writing for users who might face the same in future.

Not sure how you are adding vhost entries.

I guess this vhost entries are added automatically with help of some programming script. Do you use VhostDirective with ServerName?

<VirtualHost *:8080>
    ServerName example.domain.com
</VirutalHost>

If so, then you can use the same domain value for populating RedirectMatch field.

If you are manually adding vhost entries just write that domain URL value explicitly instead of HTTP_HOST.

Or let me know if its a different scenario.

Quacksalver answered 27/5, 2021 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.