Skip to content

Redirect http to https

Vinzenz Hersche edited this page Jun 13, 2018 · 7 revisions

It can be nice to force https by redirect all http-content to it.

This job is basicly done by the webserver itself and therefore it is a job for the administrator.

We don't like to force this, because of decrease possibilitys.

CPanel and similiar

Search in your hosting-menu for a option to enable this. There are various, we can not support all - in case of questions, ask your hoster.

Apache

Apache has two possibilitys: edit the .htaccess-file or change the config from apache itself.

.htaccess

Add those lines to the begin of your .htaccess-file, which is in your youphptube-folder:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

That's it.

Tested by author.

Apaches config file

Under debian and ubuntu, you can find the config-files under /etc/apache2/conf-enabled/ .

There, edit the config for your needs (for example: replace the url). The example starts with http (:80-section) and ends with the targeted https-connection (:443).

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

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

This solution is more proper and eventualy secure but need more expirience and edit than the .htaccess-solution.

Untested by author.

Original-source for both solutions: https://stackoverflow.com/questions/4083221/how-to-redirect-all-http-requests-to-https

Nginx

This is a redirect-example for nginx. If you use nginx, some more expirience is good anyway. The config-files are like always in /etc, what's the exact name for nginx can depend.

server {
   listen         80;
   server_name    my.domain.com;
   return         301 https://$server_name$request_uri;
}

server {
   listen         443 ssl;
   server_name    my.domain.com;
   # add Strict-Transport-Security to prevent man in the middle attacks
   add_header Strict-Transport-Security "max-age=31536000" always; 

   [....]
}

This solution is untested, source: https://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom

Clone this wiki locally