1

I have a vhost that accepts all domains and subdomains from 4 top domain names, e.g. example.com, example.org, three.example.co.uk etc.

I want the entire VHost to redirect to the exact same URL as requested, but HTTPS.

I know of this:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.all *.other *.domains
    Redirect permanent / https://example.com/
</VirtualHost>

but that won't work for subdomains, because explicit target URL.

It's so easy in nginx:

return 301 https://$host$request_uri;

so I thought Apache would have a thing for that, and it sort-of does, but this doesn't work (no Apache error, just broken redirect):

Redirect permanent / https://%{HTTP_HOST}/

Is there any way to use a domain/host variable? Bonus points for not using RewriteRule!

  • What do you have against RewriteRule ? – krisFR May 11 '15 at 19:01
  • @krisFR Nothing, but it should be unnecessary. What do you have against bonus points. You know how with RewriteRule? – Rudie May 11 '15 at 19:27
  • Don't take it bad, it was just a friendly question...you could have reason like "performance" or so...By the way i have nothing against bonus points but how could you provide this ? The best you can do would be an upvote and an accepted answer : nothing bonus here. I admit i know with RewriteRule as i have done this kind of setup many times. Also Googling gives a lot of samples, you've found one :) Glad it works for you now. have a nice day :) – krisFR May 11 '15 at 19:46
  • @krisFR Haha the bonus points were more of a mental thing. You have plenty of real points =) I don't know about perf, but it can't be all that bad. I only googled for 'without mod_rewrite' and couldn't find it. Still would be cool – Rudie May 11 '15 at 20:24
  • I only know the mod_rewrite method. If this could be achieved using only redirect i would be glad to learn about it also, just like you !! :) :) – krisFR May 11 '15 at 20:28
1

Answer in Apache SSL New Domain Redirect is good enough. (Didn't see that one while typing question.)

Solution:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com etc
    RewriteEngine on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

Using %{HTTP_HOST} in target URL seems to work fine with Apache 2.4.

Any method without rewriting, like nginx does? Or not worth it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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