1

I have an Apache2 web server running on Ubuntu 12.04. The domain of the website that I am hosting works fine, and if you go to the http version instead of the https version, it automatically redirects to the https version of the domain.

However, if I go to the http version of the ip address of the domain, it does not automatically redirect to the https version of the ip address.

There is nothing in the .htaccess or httpd.conf file.

I have tried adding a redirect for the ip specifically in the .htaccess file, but it does not work properly and the when I visit the domain, it says there are too many redirects.

I have a default-ssl file and website file in the sites-enabled folder. The default-ssl has virtual host config for port 443 and the ServerName

The website file has virtual host config for port 80, but no ServerName

Both files have the same DocumentRoot

How do enable the https redirect for the ip address as well, not only the domain?

  • why do you need this?? – ADM Jul 2 '14 at 23:45
  • My site handles very sensitive information, I don't want someone to access it through http at all. – masterhon Jul 4 '14 at 19:57
1

If you want redirect all :80 visits excluding those to http://example.com:

<VirtualHost _default_:80>
     RewriteEngine On
     RewriteCond %{HTTP_HOST} !^example.com$
     RewriteRule /.* https://example.com/ [R]
</VirtualHost>

If you want to redirect even the ones that go to http://example.com, do the following:

<VirtualHost _default_:80>
     RewriteEngine On
     RewriteRule /.* https://example.com/ [R]
</VirtualHost>

Obviously, if you have other VirtualHost stanzas, it may never reach the _default_ one.

Hope this helps!

  • Which file would I add this to? I just upgraded to apache 2.4.9 – masterhon Jul 4 '14 at 19:56
  • Replace the current VirtualHost for port 80. – Belmin Fernandez Jul 4 '14 at 20:09
  • In the file for the site in sites-enabled? – masterhon Jul 5 '14 at 20:32

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.