Its really important for new blogs or websites to deal correctly with canonical URL’s. If not then you will end up with bad SEO resulting in search engine ban. So we will see how to redirect www and non-www.
What are canonical URL’s?
Canonical urls are the search engine friendly urls. According to Google they call this process as Canonicalization which is the process of picking the best url when there are several choices, and it usually refers to home or index pages.
For example, in the view point of humans these urls are the same:
www.domain.com/ domain.com/ www.domain.com/index.html/ domain.com/index.php/
But technically all of these urls are different. Search engines treat all these urls as entirely different. A web server could return completely different content for all the urls above. When Google “canonicalizes” a url, it tries to pick the best url from that set.
The Problem?
Since search engines considers all the above urls as entirely different, the content will still remain the same in all the urls. This may lead to duplicate content issue.
Solution: Use Permanent www/non-www redirects
Choosing whether to redirect domain to completely www or without www(non-www) will avoid duplicate content. This also help to avoid the possibility of split page rank.
![]()
Before we see how to redirect, first login to your hosting cPanel and open .htaccess.
Note: If you do not have any file with name .htaccess then create one blank text file with the extension .htaccess. Also make sure you have chosen “Show hidden files and folders” before opening file manager.
Redirect non-www to www
Copy this code to .htaccess file.
# Always use www in the domain
# Replace 'domain.com' with your domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1domain.com%{REQUEST_URI} [R=301,L]
Replace domain.com with your domain name. I thought of explaining this, but it would become bit lengthy and boaring for others who do not want to know much about this. So i will create a tutorial about .htaccess and how to use this file.
Redirect www to non-www
Copy this code to .htaccess file.
# Never use www in the domain
# Replace 'domain.com' with your domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
Replace domain.com with your domain name. I use the 2nd code in my htaccess file to redirect all www request to non-www.
Let us know if you need any help with .htaccess files. Feel free to comment, it just takes seconds and makes us fell better.
{ 6 comments… read them below or add one }
i have one url tktonline.tktlocal.org when i type http://www.tktonline.tktlocal.org it should be redirect to http://tktonline.tktlocal.org
i had followed the steps but it is showing error like you don’t have a dns entry
Hello,
in the example listed for redirecting www to non,
# Never use www in the domain
# Replace ‘domain.com’ with your domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
IN the last line do i place my domain name in between http:// and %
No. There is nothing to do with the last line. You just need to edit the 2nd line by replacing domain and .com with your website name and the extension(like .net or .org etc).
Finally some code that actually works. I have tried what other people have said worked for the last 2 hours and finally came across this. It actually works. I can now get my http://www.mydomain.com/mypage to redirect to mydomain.com/mypage. Before it just kept going back to mydomain.com
Thanks!
Hello,
What if I want to redirect both the www and the non-www to a different domain entirely? Can I use both rules in an .htaccess file?
I have another broblem i wont to redirect my direct bath for the website for examble my carrint bath is http://www.brothersinstitute.com/ blog
I wont to chang it to http://www.brothersinstitute.com i tried to move the files but still not working.