User Tools

Site Tools


apache:redirect_http_to_https

This is an old revision of the document!


Apache - Redirect HTTP to HTTPS

Redirect your visitors to the HTTPS version of your website

Edit your apache configuration file (/etc/apache2/sites-enabled/website.conf and /etc/apache2/httpd.conf for example).

/etc/apache2/sites-enabled/website.conf
<VirtualHost *:80>
  [...]
  ServerName example.com
  Redirect permanent / https://example.com/
</VirtualHost>

If you only redirect, you don't even need a document root.

You can also use modrewrite, however the above method is simpler and safer. However, modrewrite below redirects the user to the page they were visiting over https, the above config just redirects to /:

/etc/apache2/sites-enabled/website.conf
<VirtualHost *:80>
  [...]
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
  </IfModule>
</VirtualHost>

And don't forget to restart Apache.

To set a redirect in your .htaccess

## Disable https on public site. Force redirect to http.
RewriteCond %{HTTPS}       on
RewriteCond %{REQUEST_URI} !^(.*)/administrator/(.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} .
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
apache/redirect_http_to_https.1468019997.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki