2 methods of Google-friendly 301 redirection: PHP, Apache .htaccess

Google dont’t like indexing a duplicated content. Have you got more domains that point to the same content? Is your site available at www and non-www version of the domain? (example: www.bitprison.net and bitprison.net point to same content) More domain point to my blog: bitprison.com, bitprison.net, bitprison.org and bitprison.hu. I need a solution for avoid duplicated content. I’ve chosen 301 redirection that google recommends for webmaters in case of duplicated content. It may help Google to determine PageRank for your site more accurately.

Now I’m sharing with you two ways of redirection. <!–break–>

Apache redirection

First you must enable mod_rewrite module in apache config file.

Content of .htaccess file or httpd.conf of apache (example):

RewriteEngine on
RewriteCond %{HTTP_HOST} !^bitprison.net [NC]
RewriteRule (.*) http://bitprison.net/$1 [L,R=301]

You need just replace from bitprison.net to your site www or non-www version of URL.

PHP code redirection

Other way is PHP redirection.

Insert this code to the beginning of index.php file (or any executable php file).


if ('bitprison.net' != $SERVER['HTTPHOST']) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://bitprison.net'.$SERVER['REQUESTURI']);


You can download it:

301_redirect.php [634 byte]

I prefer PHP version to apache config. Apache version requires mod_rewrite module and permission to override apache configuration with .htaccess file.


Read more posts by this author.