Redirect HTTP to HTTPS on default virtual host without ServerName
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.
Related videos on Youtube
highlycaffeinated
Updated on July 05, 2022Comments
-
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 toRewrite
? -
Mark Fox over 10 yearsAnother problem, your
RewriteRule
will probably never match; pretty sure you want to drop the slash:RewriteRule ^(.*) …
-
Jimmy Koerting over 10 yearsPretty 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 almost 10 yearsThe 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 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 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 about 8 yearsthis 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. about 8 yearsYou may have to add RewriteCond %{HTTPS} off after RewriteEngine On otherwise you may get a ERR_TOO_MANY_REDIRECTS
-
Zam Sunk about 8 yearsThis doesn't answer Without specifying the ServerName part of the question
-
Dunatotatos about 6 yearsSmall typo. A slash is missing, the rewriteRule should be
https://%{HTTP_HOST}/$1 [R=301,L]
-
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 over 5 yearsThis works for my setup. Also don't forget the trailing slash or it won't redirect with subfolders properly
-
kiwicomb123 about 5 yearsThanks, very helpful and much less complex.
-
David Najman almost 5 yearsIf 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.)