 |
NickSoft Linux Cookbook Quick howto's, Real live examples.
|
|
View previous topic :: View next topic |
Author |
Message |
NickSoft Site Admin

Joined: 13 Nov 2006 Posts: 22
|
Posted: Mon Nov 27, 2006 8:29 pm Post subject: 301 Redirect - domain.com->www.domain.com and ip->doma |
|
|
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:
Code: |
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:
Code: |
<?
$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:
Code: | ServerAlias 72.21.34.36 |
2. add it to index.php:
Code: | 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:
Code: | 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 |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|