Redirecting http to https

Do you have  a SSL certificated web site and your audience still connects you via  http because of their habit ? Easy way to handle this problem is redirecting http requests to https. By doing that any http requests are redirected to the https. To do that we will use Apache web server (httpd RHEL,CentOS).

Apache is the world’s most used web server software. It has many features loadbalancing, cgi support, headers and content rewriting, URL rewriting etc,. In this post we will use URL rewriting capability of Apache, which is supported by mod_rewrite.

mod_rewrite module provides flexible and powerful way to URLs using an unlimited number of rules. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.

You can add the configuration below either .htacces or apache2.conf (httpd.conf on RHEL, CentOS).

#Redirect permanently any request, which comes from Port 80(http) to https.
<IfModule mod_rewrite.c>
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://manintheit.org/$1 [R=301,L]
</IfModule>

Do not forget to enable mod_rewrite module. You can check with apache2ctl or httpd utility.

On Debian, Ubuntu system.

root@debian:/etc/apache2# apache2ctl -M|grep rewrite

On RHEL, CentOS system.

[root@centos7 media]# httpd -M| grep rewrite

You can track the requests http to https with curl -v.

redirection