Htaccess redirect

.htaccess files can be used to make redirects. Below are some common examples.

Redirect from old domain to new domain

You can use the code below if you have changed domain and want users on your old domain to be redirected to your new domain.

RewriteEngine on
RewriteRule ^(.*)$ http://www.your-new-domain.com/$1 [R=301,L]

If a user visits http://www.olddomain.com/articles.php he will be redirected to the same page on your-new-domain.com. The R=301 part make it a permanent redirect by sending a “301 Moved Permanently” status code to the user’s browser. If the user happens to visits the same page on your old domain, the browser will automatically redirect to the new domain.

Redirect to www

So you only want users to visit www.mydomain.com and not domain.com? Htaccess redirects can solve this too. Copy the code below into a .htaccess for redirecting all visits to your www domain.

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