With the increasing number of SSL certificates implemented, there is a recurring question of how access via https can be forced in order to take advantage of the certified secure connection. In this article we show you how you can forward http requests to https through different ways.
It is always important to remember that changing files, such as those mentioned below, must be preceded by a backup / copy of them so that they can be restored if necessary.
Using an .htaccess file (Apache)
You can configure the redirect by inserting the following code in a '.htaccess' file present in the directory where the contents that will be accessed through the address you want to forward are located.
To redirect all requests:
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.yourdomain.com/$1
To redirect only a particular domain, you should insert:
RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.net RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.yourdomain.com/$1
If you only want access to the contents of a folder to be forced to https, then you should use:
RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} folder RewriteRule ^(.*)$ https://www.yourdomain.com/directory /$1
Using a configuration file (nginx)
In order to configure the redirect from http to https on an nginx web server, you must first identify the configuration file of the domain you want to redirect. By default, the configuration file will be in one of the following directories:
/etc/nginx/conf.d/ /etc/nginx/sites-available/
In the configuration file (Eg: your_domain.conf) you must insert or edit a 'server' block using the following example:
server { listen 443 ssl; server_name domain.com www.domain.com; .... } # Redirect to https server { listen 80; server_name domain.com www.domain.com; return 301 https://domain.com$request_uri; }
In this configuration, all requests waiting on port 80, for the specific domain, are read and subject to a redirection of type 301 (permanent) to the same address and arguments, but on an https connection (port 443).
After carrying out the configuration in, you must perform a check that it is correctly defined. To do so, run the following command to validate the configuration:
nginx -t
If you get 'OK', you can reload the nginx configuration so that it can be loaded, by executing the following command:
systemctl reload nginx
Using a web.config configuration file (IIS)
In a Windows environment, we have the web.config file for carrying out configurations as the desired, in this case redirecting an access from http to https.
In the web.config file present in the main directory where you have the contents of the address you want to forward, you will only need to add the following bit of code:
<system.webServer> <rewrite> <rules> <rule name="Redirect from HTTP to HTTPS" enabled="true"> <match url="(.*)" ignoreCase="false"/> <conditions> <add input="{HTTPS}" pattern="off"/> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent"/> </rule> </rules> </rewrite> </system.webServer>
Using administration panel cPanel
In cPanel, setting up a redirect from http to https is quite simple and intuitive.
1) In the cPanel where you have the address you want to forward configured, after login, you should access the ‘DOMAINS’ block.
2) Then click on the ‘Domains’ option.
3) The 'Domains' section contains all the domains (main, aliases or addon) configured in the account. In front of each of the domains, the option ‘Force HTTPS redirect’ is available. If desired, just activate / deactivate this option for the address(es) for which you want to activate / deactivate the redirect from http to https.
When the option mentioned in point 3 is activated, the system will automatically insert content into the .htaccess file present in the main directory, where the contents accessible through the domain for which the redirection was activated are. So, in order to keep this configuration functional, you should not remove / change the contents of the .htaccess file without having a thorough knowledge of the changes made.
Using administration panel Plesk
In the Plesk administration panel, like other actions available, the configuration of a redirect from http to https can be performed very quickly and simply.
1) In the 'Websites & Domains' section of your Plesk admin panel, find the section regarding the domain you want to configure and select the 'Hosting & DNS' tab.
2) Click on ‘Hosting Settings’ to access the main account settings.
3) Enable the ‘Permanent SEO-safe 301 redirect from HTTP to HTTPS’ option and ensure that you also have the ‘SSL / TLS Support’ option enabled and that you have an SSL certificate selected in the drop down menu available.