Redirect Webpages Using .htaccess
Developers How To

Redirect Webpages Using .htaccess

Here is a short tutorial about how to redirect your website using .htaccess file. Before I start, let me tell you what is a .htaccess file. .htaccess file is a small text document. It gives you the ability to interact with Apache on an individual domain-to-domain and directory-to-directory basis or in more technical term we can say it  is a directory-level configuration file supported by several web servers, which helps us in management of web server configuration. Please create a backup of original .htaccess file before following below instructions.

Redirecting non www to www website

This is for redirecting your non www website (e.g. example.com) to www address (e.g. www.example.com)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301, NC]

Redirecting www to non www website

This is for redirecting your www website (e.g. example.com) to non www address (e.g. www.example.com)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mynewwebsite.com$ [NC]
RewriteRule ^(.*)$ http://mynewwebsite.com/$1 [L,R=301, NC]

Hope the guide helped you in what you were looking for when you reached this page ;). If you stuck somewhere or anything goes wrong, please let me know in the comments section, I will try my best to answer every query.