Htaccess Redirects

Redirects enable us to control the flow of visitors. you can protect directories and redirect the traffic to other pages. For all type of redirects using the mod_rewrite directive, it is necessary to have the RewriteEngine enabled in your server.

Redirect filepaths

To redirect a filepath to another filepath

RewriteEngine on Redirect /foldername/filename.html http://mydomain.com/otherfoldername/filename.html

Redirect to another page

To redirect one page to another page

RewriteEngine on Redirect /old-filename.php http://mydomain.com/foldername/new-filename.php

Redirect page to subdirectory

To redirect a page to a subdirectory

RewriteEngine on Redirect /index.php http://mydomain.com/foldername

Redirect www to non-www

To redirect http://www.mydomain.com to http://mydomain.com

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

Redirect non-www to www

To redirect http://mydomain.com to http://www.mydomain.com

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