301 redirect from non www to www

danprobo

Member
Messages
163
Reaction score
4
Points
18
We know that search engines have the ability to determine the www and non-www versions are the same and combine the results but occasionally it fails and we see both versions is indexed, and this is bad for the site owner since link benefit (PR/anchor text) is shared over two sites.

Here's a few step to resolve this problems by editing the .htaccess file and write a permanent 301 redirect script on it:

1. Go to your cPanel.

2. Then File Manager

3. Find your .htaccess file on public_html.

4. Check the checkbox on .htaccess file.

5. On toolbar click Edit.

6. Write this script on your .htaccess file (the red ones and change the "example.com" with your domain name):

For non www to www:
------------------
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]


For www to non www:
------------------
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]


7. After write the script then save it.
8. Now try visit your site.


Hope this helpful :naughty:

Cheers :biggrin:
 

nirajkum

New Member
Messages
159
Reaction score
0
Points
0
thanks danpro it was reallly useful . But the problem is that some hosting site dont provide cpanel
 

danprobo

Member
Messages
163
Reaction score
4
Points
18
A high PR website always do that, they will choose displaying their website as www or non www. Ya x10Hosting is the best :)
 

mario8

New Member
Messages
5
Reaction score
0
Points
0
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

PHP:
<?
$ref=$_SERVER['QUERY_STRING'];
if ($ref!='') $ref='?'.$ref;
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://newdomain.com/'.$ref);
exit();
?>

PHP:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.ru/newdir/newpage.htm");
exit();
?>
 
Last edited:
Top