30 Apr
How to redirect url with or without www in htaccess
Normally every website domain is accessible with or without www (mydomain.com or www.mydomain.com). We will get the same pages & there will be no difference. But Google will consider it as duplicated content and it will affect your Search Engine Optimization ranking. So we should define a preferred domain method with the help of htaccess. It will redirect the access to either with or without www. Alternatively, add canonical URL ( <link rel=”canonical” href=”http://www.mydomain.com” /> ) in your page head and don’t forget to choose a preferred domain in Google Webmaster Tools.
Here i’m going to show you how to define a preferred domain method in htaccess.
Redirect mydomain.com to www.mydomain.com:
RewriteEngine On RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
Redirect www.mydomain.com to example.com:
RewriteEngine on RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
Redirect IP to www.example.com:
RewriteEngine on RewriteCond %{HTTP_HOST} ^12\.34\.56\.789$ RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
Latest posts by Sanju (see all)
- Top 10 Features in Angular 18 You Need to Know - June 25, 2024
- Specialities of Kerala’s agricultural and religious festival Vishu - November 25, 2022
- Why Kerala is known as God’s own country – Part 2 - June 20, 2019