.htaccess - redirect http to https with or without www

This Blog post is about to discuss about how to redirect http to https with or without www by using htaccess. Before start first a small introduction of:

  1. .htaccess
  2. https

.htaccess

.htaccess is a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software. These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance, if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hotlink prevention.

HTTPS

HTTP Secure (HTTPS) is an extension of the Hypertext Transfer Protocol (HTTP) for secure communication over a computer network and is widely used on the Internet. In HTTPS, the communication protocol is encrypted using Transport Layer Security (TLS), or formerly, its predecessor, Secure Sockets Layer (SSL). The protocol is therefore also often referred to as HTTP over TLS, or HTTP over SSL.

The principal motivation for HTTPS is authentication of the accessed website and protection of the privacy and integrity of the exchanged data while in transit. It protects against man-in-the-middle attacks. The bidirectional encryption of communications between a client and server protects against eavesdropping and tampering of the communication. In practice, this provides a reasonable assurance that one is communicating without interference by attackers with the website that one intended to communicate with, as opposed to an impostor.

Read Also: A Complete Guide to Migrating your WordPress Website to a New Web Host

.htaccess – Automatically redirect http to https with or without www

If you have a secure certificate (SSL) on your website, you can automatically redirect visitors to the secured (HTTPS) version of your website to make sure their information is protected.

To successfully redirecting http://www and non www to HTTPS

You just add the following code to .htaccess, and it’s working now.

# BEGIN HTTPS Redirection Plugin
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# END HTTPS Redirection Plugin

Note this will redirect using the 301 ‘permanently moved’ redirect, which will help transfer your SEO rankings.

To redirect using the 302 ‘temporarily moved’ change [R=302,L]

Read Also: Errors and Exceptions in Python

Enter your personal email address below to subscribe our Newsletter:

Enter your email address:

Your information is 100% safe and will not be shared with anyone else. You can unsubscribe with one click at any time.

LEAVE A REPLY

Please enter your comment!
Please enter your name here