How to Redirect HTTP to HTTPS in Apache - MS TV Life.COM

How to Redirect HTTP to HTTPS in Apache

How to Redirect HTTP to HTTPS in Apache

We hope this post helped you to find out  How to Redirect HTTP to HTTPS in Apache

Apache HTTP server is among the hottest net servers on the planet. It’s an open-source and cross-platform HTTP server that powers a big proportion of the Web’s web sites. Apache supplies many highly effective options that may be prolonged by way of extra modules.

In case you are a web site proprietor or system administrator, chances are high that you just’re coping with Apache regularly. Some of the widespread duties you’ll doubtless carry out is redirecting the HTTP visitors to the secured (HTTPS) model of your web site.

How to Set Up Nginx Server Blocks on Debian 10

Not like HTTP, the place requests and responses are despatched and returned in plaintext, HTTPS makes use of TLS/SSL to encrypt the communication between the consumer and the server.

There are numerous benefits of utilizing HTTPS over HTTP, equivalent to:

  • All the information is encrypted in each instructions. Because of this, delicate info can’t be learn if intercepted.
  • Google Chrome and all different in style browsers will mark your web site as secure.
  • HTTPS means that you can use the HTTP/2 protocol, which considerably improves the positioning efficiency.
  • Google favors HTTPS web sites. Your website will rank higher if served through HTTPS.

This information covers methods to redirect the HTTP visitors to HTTPS in Apache.

There are a number of methods to redirect to HTTPS in Apache. When you’ve got root entry to the Linux server the place Apache runs, the popular approach is to arrange the redirection within the area’s digital host configuration file. In any other case, you’ll be able to arrange the redirection within the area’s .htaccess file.
Some management panels, equivalent to cPanel means that you can pressure HTTPS redirection with just a few mouse clicks.

Redirect HTTP to HTTPS utilizing Digital Host #

Apache Digital Hosts defines the settings of a number of domains hosted on the server. Within the digital host directive, you’ll be able to specify the positioning doc root (the listing which incorporates the web site recordsdata), create a separate safety coverage for every website, use totally different SSL certificates, configure redirection, and far more.

Sometimes when an SSL certificates is put in on a website, you’ll have two digital host directives for that area. The primary one for the HTTP model of the positioning on port 80, and the opposite for the HTTPS model on port 443.

In Red-Hat based distros such as CentOS and Fedora, virtual host files are stored in the /etc/httpd/conf.d. While on Debian and its derivatives like Ubuntu the files are stored in the /etc/apache2/sites-available directory.

To redirect a web site to HTTPS, use the Redirect directive as proven within the instance beneath:

<VirtualHost *:80> 
  ServerName example.com
  ServerAlias www.example.com

  Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName example.com
  ServerAlias www.example.com

  Protocols h2 http:/1.1

  # SSL Configuration

  # Other Apache Configuration

</VirtualHost>

Let’s clarify the code. We’re utilizing have two digital host directives, one for HTTP and one for the HTTPS model of the positioning.

  • VirtualHost *:80 – The Apache server listens for incoming connections on port 80 (HTTP) for the desired area.
  • VirtualHost *:443 – The Apache server listens for incoming connections on port 443 (HTTPS) for the desired area.

The ServerName and ServerAlias directives are specifying the digital host’s domains. Be sure you change it along with your area identify.

The highlighted line, Redirect everlasting / https://instance.com/ contained in the HTTP digital host, redirects the visitors to the HTTPS model of the positioning.

Sometimes you additionally wish to redirect the HTTPS www model of the positioning to the non-www or vice versa. Right here is an instance configuration:

<VirtualHost *:80> 
  ServerName example.com
  ServerAlias www.example.com

  Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName example.com
  ServerAlias www.example.com

  Protocols h2 http:/1.1

  <If "%{HTTP_HOST} == 'www.example.com'">
    Redirect permanent / https://example.com/
  </If>

  # SSL Configuration

  # Other Apache Configuration

</VirtualHost>

The code contained in the HTTPS digital host (the highlighted traces ) is checking whether or not the request header incorporates the www area and redirects to the non-www model.

Everytime you make modifications to the configuration recordsdata it is advisable to restart or reload the Apache service for modifications to take impact:

Redirect HTTP to HTTPS utilizing .htaccess #

.htaccess is a configuration file on a per-directory foundation for the Apache webserver. This file can be utilized to outline how Apache serves recordsdata from the listing the place the file is positioned and to allow/disable extra options.

Normally, the .htaccess file is positioned within the area root listing, however you’ll be able to produce other .htaccess recordsdata within the subdirectories.

This methodology requires the mod_rewrite module to be loaded on the Apache server. This module is loaded by default on most servers. If potential, want making a redirection within the digital host as a result of it’s less complicated and safer.

To redirect all HTTP visitors to HTTPS, open the foundation .htaccess file, and add the next code to it:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

Here is what the code means:

  • RewriteEngine On – enables the Rewrite capabilities.
  • RewriteCond %{HTTPS} off – checks for HTTP connection, and if the condition is met, the next line is executed.
  • RewriteRule ^(.*)$ https://example.com/$1 [L,R=301] – redirect HTTP to HTTPS with status code 301 (Moved Permanently). Make sure you change the domain name.

The example below has an additional condition that checks whether the request begins with www. Use it to force all visitors to use the HTTPS non-www version of the site:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

When modifying .htaccess file, you do not want to restart the server as a result of Apache reads the file on every request.

Conclusion #

In Apache, the popular approach to redirect HTTP to HTTPS is to configure the 301 redirect within the area’s digital host.

We hope the How to Redirect HTTP to HTTPS in Apache help you. If you have any query regarding How to Redirect HTTP to HTTPS in Apache drop a comment below and we will get back to you at the earliest.

We hope this post helped you to find out  How to Redirect HTTP to HTTPS in Apache  . You may also want to see – How to Configuring the Apache Error and Access Logs
.

Copy link
Powered by Social Snap