6

I have the following conf files:

file1:

NameVirtualHost  123.45.67.890:80

<VirtualHost 123.45.67.890:80>
    ServerName example.com

    RedirectPermanent / https://example.com/

#   RewriteEngine On
#   RewriteCond %{SERVER_PORT} !^443$
#   RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

#   SSLRequireSSL
#   Redirect permanent /secure https://example.com/

#   Redirect / https://example.com/
</VirtualHost>

As you can see from the commented out lines, I have tried several approaches.

file2:

NameVirtualHost 123.45.67.890:443

<VirtualHost 123.45.67.890:443>
    DocumentRoot "/opt/www/example-docroot"
    ServerName example.com
    DirectoryIndex index.html
    SSLEngine on

    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    SSLCertificateFile /etc/httpd/conf.d/ssl/example.com/csr.example.2011.pem.blade
    SSLCertificateKeyFile /etc/httpd/conf.d/ssl/example.com/nokey.example.2011.pem
    SSLCACertificateFile /etc/httpd/conf.d/ssl/example.com/CA.blade.2011.csr

    <Files ~ "\.(cgi|shtml|phtml|php3?)$">
        SSLOptions +StdEnvVars
    </Files>
    <Directory "/etc/httpd/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>
</VirtualHost>

Some thing removed for simplicity, names changed to protect the innocent.

I update these files on the server /etc/httpd/vhosts

then run

apachectl restart

Which does give me these warnings:

[warn] NameVirtualHost 123.45.67.890:80 has no VirtualHosts
[warn] NameVirtualHost 123.45.67.890:80 has no VirtualHosts

We have numerous vhosts running from this server. The above configs seem to be aligned, so I don't think these warnings are applying here. Maybe wrong.

Updating the default 80 (http) page in my browser, always shows the default http page.

Any suggestions, on how to get the redirect to work?

8 Answers8

21

Many examples work on specific configurations. This one always works, no matter which configuration your Apache server uses:

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]
BVB Media
  • 226
  • 1
  • 3
  • What file do you you add this to? – Richard Jun 26 '17 at 14:28
  • 2
    @Richard Just got this working on CentOS, I created a new redirect.conf file under httpd/conf.d and added it within a directive. I am using this with django and had to remove the trailing slash before the `$1` in the redirect, to avoid an extra slash. – trpt4him Jun 27 '17 at 15:04
  • I tried many other variations on this and this is the only one that has worked for me if using non-standard ports and variables, and not hard coding the host. Add the condition using the custom port ( !10443, in my case), and the rewritten host part: https://${HTTP_HOST}:10443/$1 – gregthegeek Oct 17 '17 at 23:54
12

I had faced exactly the same issue few days back. I tried the following in my VirtualHost config (applicable for http port 80) in apache httpd.conf file that worked.

<Virtualhost *:80>
ServerAdmin webmaster@site.com
ServerName site.com
ServerAlias site.com www.site.com

RedirectMatch permanent ^(.*)$ https://www.site.com$1
</Virtualhost>

This works like charm and you don't need any config anywhere else or any extra module.

Kris
  • 271
  • 2
  • 3
  • 11
webstat
  • 541
  • 1
  • 3
  • 6
1

Our issue was config file precedence. In short, the HTTP VirtualHost in 000-default.conf was overriding the settings we had created in our own config file.

This is confusing to anyone who knows the meaning of the word "default" which means something only used if it's not defined elsewhere. We foolishly assumed that 000-default.conf would only provide configuration if it was not defined in another config file.

We were wrong. Apache just uses the config in the first file it encounters based on file name in alphanumeric order. It moves on to the next file if it cannot find a matching VirtualHost in that file, which causes fun confusion if you don't know how it works but is kind of neat once you do.

John T.
  • 131
  • 1
  • This is true, that's why I almost remove either this config or just use the default config to only host one page per container :) – djdomi Jul 16 '21 at 06:18
1

Better use .htaccess for this (if possible), no need to mess with the apache config files. Add these lines to the beginning of your .htaccess.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
drcelus
  • 1,254
  • 4
  • 14
  • 28
  • 10
    It's definitely *not* better to use `.htaccess` because Apache ends up doing unnecessary work (like mapping URLs to filesystem paths) before performing the redirect as hinted at in [point 1 here](http://wiki.apache.org/httpd/RewriteHtaccessIgnored). (Also, [the docs recommend against using mod_rewrite for simple redirection](http://httpd.apache.org/docs/current/rewrite/avoid.html#redirect)) – nickgrim Aug 23 '12 at 08:50
  • Nice point and useful information. My answer may still apply to those that don't have access to the apache config files however. – drcelus Aug 23 '12 at 11:17
  • Odd. Even this doesn't work. Something else is going on... In any case, I'd rather do with a conf command than this, but this is a good solution as a fall back plan. – James John McGuire 'Jahmic' Aug 24 '12 at 03:28
  • What file do you add this to? – Richard Jun 26 '17 at 14:29
1

I had the same issue on apache 2.4 on centos 7 and the fix that worked for me without using the RewriteEngine was to add :443 to the ServerName directive under the VirtualHost for https.

<VirtualHost 123.45.67.890:443>
DocumentRoot "/opt/www/example-docroot"
ServerName example.com:443
0

Clearing the cache of my browser fixed the issue.

ndemou
  • 1,315
  • 3
  • 17
  • 28
0

I am using wamp server in windows(Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.12). I have successfully installed SSL certs. Now https is working without any issue. But http redirection is not working.


<VirtualHost *:80>
ServerName test.com
ServerAlias www.test.com
Redirect permanent / https://test.com/

</VirtualHost>

<VirtualHost *:443>
ServerName test.com
ServerAlias www.test.com
DocumentRoot "C:/wamp64/www/test"
<Directory "C:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile "C:/wamp64/bin/apache/apache2.4.41/conf/key/fullchain.pem"
SSLCertificateKeyFile "C:/wamp64/bin/apache/apache2.4.41/conf/key/privkey.pem"
SSLCertificateChainFile "C:/wamp64/bin/apache/apache2.4.41/conf/key/fullchain.pem"
</VirtualHost>

Could you please help me figure out this one?

Pram
  • 1
  • 1
0

I think last time this happened to me it may have been something weird like NameVirtualHost *:80/NameVirtualHost *:443 and/or Listen 80/Listen 443 were somewhere other than ports.conf for apache2.

Here is what I have in VirtualHost to redirect to https for all pages except for press (had a video that couldn't be served https so had to redirect back to http otherwise people would get ugly warnings about the page being unsafe):

<VirtualHost *:80>
    ServerName example.com
    RewriteEngine On

    RewriteCond %{REQUEST_URI} !^/press/.*
    RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]
</VirtualHost>
sss
  • 51
  • 2