Apache Redirect to HTTPS
imprimir- 0
Although installing an SSL certificate on a website provides the possibility of accessing it with the secure https:// protocol, the protocol is not used by default. To make sure that the website is accessed using the https:// protocol by default, you will need to set up an automatic redirect.
If you have a control panel installed over Apache, you will need to set up redirects in the panel itself and not on the server to avoid redirect loops or incorrect module execution. Check out our guide on how to set up a HTTPS redirect in cPanel here.
An Apache redirect should be used if you are not using cPanel or any other control panel or GUI (graphical user interface).
- Enabling the redirect in the Virtual Host file
- Enabling the redirect in the .htaccess file (previously created in the document root folder)
- Using the mod_rewrite rule in the Virtual Host file
Enable Apache Redirect in the Virtual Host
Enabling the redirect in the Virtual Host file is safer and simpler than other options presented in this guide. The configuration is also similar for all systems. It involves adding a specific piece of code to the Virtual Host file. Usually, there are two Virtual Host files on Apache if an SSL certificate is installed: one is for the non-secure port 80, and the other is for the secure port 443.
- Locate the VirtualHost configuration for port 80 by running the following command:
- for Debian-based servers (Ubuntu):
apachectl -S - for RHEL-based servers (CentOS):
httpd -S
- for Debian-based servers (Ubuntu):
- The redirect to HTTPS can be enabled in the Virtual Host file for port 80. If you would like to force HTTPS for all web pages, you can use the following set of directives:
- to redirect everything to https://yourdomain.com:
<VirtualHost *:80>
ServerName yourdomain.com
Redirect permanent / https://yourdomain.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
...
</VirtualHost> - to redirect everything to https://www.yourdomain.com:
<VirtualHost *:80>
ServerName www.yourdomain.com
Redirect permanent / https://www.yourdomain.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
...
</VirtualHost> - to redirect a specific directory (/secure in our case):
<VirtualHost *:80>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
Redirect permanent /secure https://yourdomain.com/secure
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
...
</VirtualHost>
Debian-based:
sudo service apache2 restart
RHEL-based:
sudo service httpd restart - to redirect everything to https://yourdomain.com:
Use .htaccess to Redirect to HTTPS
</VirtualHost>
<Directory /usr/local/apache2/htdocs>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
After that, it will be possible to add configurations to .htaccess files.
If the command returns nothing, the .htaccess file should be created in the main document root folder.
- to redirect everything to https://yourdomain.com:
Redirect permanent / https://yourdomain.com - to redirect everything to https://www.yourdomain.com:
Redirect permanent / https://www.yourdomain.com - to redirect only a specific directory (/secure in our case):
Redirect permanent /secure https://yourdomain.com/secure
Note: it’s recommended that you place this code at the very top of the .htaccess file so that it can overwrite other conflicting codes.
Use Apache Rewritecond - mod_rewrite Rule
If the module is enabled, the following message will be sent by the server:
Make sure it is not commented. If this line is not in in the main configuration file, install the rewrite module by running this command:
- to redirect everything to https://yourdomain.com:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Note: The code for www is the same as for non-www, however, for it to work correctly, the ServerName needs to be set as www in the VirtualHost configuration code. - to redirect a specific directory (/secure in our case):
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?secure/(.*) https://%{SERVER_NAME}/secure/$1 [R=301,L]
Note: To set a temporary redirect, change the 301 status code (permanent) to 302 (temporary) on the R-flag.