Saturday, October 18, 2014

How to redirect non-www to www with .htaccess

Leave a Comment
Hallo, Good Evening dear,
Today we will learn about "How to redirect non-www to www with .htaccess", in this case for the first word i say is .htaccess.

According to wikipedia.com, .htacces file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration. They are placed inside the web tree, and are able to override a subset of the server's global configuration for the directory that they are in, and all sub-directories.
The original purpose of .htaccess—reflected in its name—was to allow per-directory access control, by for example requiring a password to access the content. Nowadays however, the .htaccess files can override many other configuration settings including content type and character set, CGI handlers, etc.

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file, just do it like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This will redirect any requests to http://example.com to http://www.example.com. There are several benefits from doing that:
  • It will avoid duplicate content in Google
  • It will avoid the possibility of split page rank and/or split link popularity (inbound links).
  • It's nicer, and more consistent.
 If you want to do the opposite, the code is very similar, and it's will be spesificate or live respon to your domain like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]
In this case we are explicitly typing the domain name. I'm sure it's possible to do it in a generic way, but I haven't had the time to work one out and test it. So remember to change 'my-domain' with your domain name.
thank's for read this :)

0 comments :

Post a Comment