Redirect between www and non-www with apache and htaccess

From The IT Community
Jump to: navigation, search




If you want to do really good SEO (search engine optimization) then you should always redirect to either the www or the non-www version of your website (e.g. www.theitcommunity.com or theitcommunity.com). Otherwise your sites will be found twice which might count as duplicate content which gets punished by Google.

On a Linux system with apache you can do this by placing a .htaccess file into the root of your website and edit it.


Here is how to redirect between www and non-www with apache and the .htaccess file:


  • Version 1: Always redirect to non-www:

Put the following into the .htaccess file:

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

Of course, you will have to replace theitcommunity.com with your URL.


  • Version 2: Always redirect to www:

Put the following into the .htaccess file:

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


Here you don´t have to replace anything.


After you created the file, you just need to reload the page in your browser and you will see the change immediately.



Was this article helpful? Then please donate to keep The IT Community alive...

If you found this article helpful please share it, comment and help others by writing your own article.






Translate this page:




Articles found in the same category:
(max. 20 shown)