Redirect HTTP to HTTPS on default virtual host without ServerName

185,207

Solution 1

Try adding this in your vhost config:

RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]

Solution 2

Both works fine. But according to the Apache docs you should avoid using mod_rewrite for simple redirections, and use Redirect instead. So according to them, you should preferably do:

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

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost>

The first / after Redirect is the url, the second part is where it should be redirected.

You can also use it to redirect URLs to a subdomain: Redirect /one/ http://one.example.com/

Solution 3

This is the complete way to omit unneeded redirects, too ;)

These rules are intended to be used in .htaccess files, as a RewriteRule in a *:80 VirtualHost entry needs no Conditions.

RewriteEngine on
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

Eplanations:

RewriteEngine on

==> enable the engine at all

RewriteCond %{HTTPS} off [OR]

==> match on non-https connections, or (not setting [OR] would cause an implicit AND !)

RewriteCond %{HTTP:X-Forwarded-Proto} !https

==> match on forwarded connections (proxy, loadbalancer, etc.) without https

RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

==> if one of both Conditions match, do the rewrite of the whole URL, sending a 301 to have this 'learned' by the client (some do, some don't) and the L for the last rule.

Share:
185,207

Related videos on Youtube

highlycaffeinated
Author by

highlycaffeinated

Updated on July 05, 2022

Comments

  • highlycaffeinated
    highlycaffeinated almost 2 years

    On my apache server I'd like to be able to redirect all incoming http requests to the equivalent https request. The catch is that I'd like to be able to do this for my default virtual host without specifying the ServerName and have the redirect work with whatever server name appeared in the request url. I'm hoping for something like this:

    NameVirtualHost *:80
    <VirtualHost *:80>
        RedirectPermanent / https://%{SERVER_NAME}/
        ...
    </VirtualHost>
    

    Is this possible using Redirect or will I have to resort to Rewrite?

  • Mark Fox
    Mark Fox over 10 years
    Another problem, your RewriteRule will probably never match; pretty sure you want to drop the slash: RewriteRule ^(.*) …
  • Jimmy Koerting
    Jimmy Koerting over 10 years
    Pretty sure I won't. You missed the / syntax of the target including the 'L' flag. The other way is doing it like Jon Lin.
  • Doktor J
    Doktor J almost 10 years
    The RewriteCond is completely superfluous in this case; since the VirtualHost is already defined as <VirtualHost *:80>, %{SERVER_PORT} will never be 443 in the first place so the condition will always match.
  • Joseph Orlando
    Joseph Orlando over 9 years
    * You may need to add mod_rewrite. For ubuntu or debian-based hosts, the following would work: sudo a2enmod rewrite which would stop any configtest / apache2 configuration errors. (Which a stock setup would receive, provided you use the vhost additions provided above)
  • Joseph Orlando
    Joseph Orlando over 9 years
    * You may need to add mod_rewrite. For ubuntu or debian-based hosts, the following would work: sudo a2enmod rewrite which would stop any configtest / apache2 configuration errors. (Which a stock setup would receive, provided you use the vhost additions provided above)
  • EsseTi
    EsseTi about 8 years
    this works only for the main domain (e.g http://mywebiste.com -> https://mywebiste.com) what if i've also subdomaind (http://blog.mywebiste.com->https://blog.mywebiste.com) ?
  • Max S.
    Max S. about 8 years
    You may have to add RewriteCond %{HTTPS} off after RewriteEngine On otherwise you may get a ERR_TOO_MANY_REDIRECTS
  • Zam Sunk
    Zam Sunk about 8 years
    This doesn't answer Without specifying the ServerName part of the question
  • Dunatotatos
    Dunatotatos about 6 years
    Small typo. A slash is missing, the rewriteRule should be https://%{HTTP_HOST}/$1 [R=301,L]
  • Jon Lin
    Jon Lin about 6 years
    @Dunatotatos in the vhost, the URI contains a leading /, but in an htaccess file, the / prefix is removed. If the rule was in an htaccess file, we'd indeed need a / before the $1
  • vdidxho
    vdidxho over 5 years
    This works for my setup. Also don't forget the trailing slash or it won't redirect with subfolders properly
  • kiwicomb123
    kiwicomb123 about 5 years
    Thanks, very helpful and much less complex.
  • David Najman
    David Najman almost 5 years
    If you want to redirect example.com to example.com, don't forget to put the SSL configuration into VirtualHost: <VirtualHost *:443> ServerName example.com SSLEngine on SSLCertificateFile ... SSLCertificateKeyFile ... Redirect permanent / example.com </VirtualHost> or it will redirect via default 443 virtualhost with not correct SSL certificate. Also don't forget the trailing slash or it won't redirect with subfolders properly (as vdidxho mantioned.)