———

 
Translations of this page:

301 Redirect to www

Using http redirect (code 301 - Moved Permanently) is useful because search engines gives negative credits for using multiple domains on the same site (mirror sites doesn't count). Other issue is when you have domain “examle.com” and both “example.com” and “www.example.com” are valid. Most of you know that one of factors that search engines use to assess site importance is so called back-links. However some site link to “example.com” and other to “www.example.com”. This way search engine rankings will be divided between “www.example.com” and “example.com”. If you redirect one domain to the other using 301 redirect search engine will give all credits to it.

Here is how to prepare virtual hosts for redirection:

namevirtualhost *:80

#set up an alias virtual host:
<VirtualHost *:80>
  DocumentRoot "/webroot/301redirect"
  ServerName 72.21.34.34
  ServerAlias 72.21.34.35
#we want to point sitealias.com to realsite.com
  ServerAlias sitealias.com
#realsite.com will redirect to www.realsite.com
  ServerAlias realsite.com
# put here other aliases if you need to

  DirectoryIndex index.php index.html

#  put CustomLog if you need to log redirects
  <Directory "/webroot/301redirect">
    AllowOverride AuthConfig Indexes Limit Options
    Options Indexes FollowSymlinks MultiViews

    RewriteEngine On
    RewriteRule ^(.*)$  index.php

    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

#this is virutal host for www.realsite.com
<VirtualHost *:80>
  DocumentRoot "/webroot/realsite"
  ServerName www.realsite.com

  <Directory "/webroot/realsite">
    AllowOverride AuthConfig Indexes Limit Options FileInfo
    Options Indexes FollowSymlinks MultiViews

#put here your site configuration

    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

put one file in /webroot/301redirect called index.php:

<?
$uri=$_SERVER["REQUEST_URI"];
if($uri=='')$uri='/';
$url='http://www.'.($_SERVER['HTTP_HOST']).$uri;
switch($HTTP_HOST){
  case '72.21.34.34': $host='www.layeredtech.com'; break;
  case 'sitealias.com': $host='www.realsite.com'; break;
// put here other aliases
  default: $host="www.$HTTP_HOST";
}
$url="http://$host$uri";
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$url);
exit;
?>

to add new ip address or site you must: 1. add it to first virtual host as alias:

ServerAlias 72.21.34.36

2. add it to index.php:

case '72.21.34.36': $host='www.layeredtech.com'; break;

to redirect mysite.com to www.mysite.com you must: 1. add it as alias in first virtual host:

ServerAlias mysite.com

2. you DON'T need to add it to index.php - To put www. before host is default action for redirection virtual host

 
lcb/webserver/301-redirect-to-www.txt · Last modified: 09.22.2008 14:39 by npelov
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki