Noob Flash Rss

Improve the SEO of your website in one step

0

Posted by Aaron | Posted in PHP, SEO, Tutorials | Posted on 23-08-2009

Search Engine Optimization (SEO) is a hot topic these days. In an ailing economy, free website traffic is becoming much more important and the competition is more fierce than ever! There are many things that you could probably improve about your website that would eventually lead to improved website SEO, but there is one very important thing that is often overlooked.

If your website does not redirect the www and non-www version of your website to the same www or non-www url, you are not maximizing the SEO capabilities of your website. For example, if you visit http://noobflash.com, you will see that the url immediately redirects to http://www.noobflash.com. If this is not done, search engines will consider the www and non-www versions of your website url as two separate websites. As you can imagine, this can damage your SEO efforts greatly! It’s up for debate as to whether www or no-www is best, but most people agree that is really comes down to personal preference. I decided to stick with the traditional www, but many of you may not like that for your website.

Luckily, the Noob is here to help you get out of this mess! I will walk you through two different ways of fixing this issue. Both options can be configured to use either www or no-www.

Option 1 – PHP 301 Redirect
This option will obviously only work if you use PHP, but you could take the same concept and apply it to any programming language. This option also redirects http://www.yourwebsite.com/index.php to http://www.yourwebsite.com/, which is another good idea.

<?php
        // SET THIS TO THE URL THAT YOU WANT TO USE (with or without www)
	$url = 'http://www.yourwebsite.com';

	if ( $_SERVER['REQUEST_URI'] == str_replace('http://' . $_SERVER['HTTP_HOST'], '', $url.'/index.php' )) {
		header('HTTP/1.1 301 Moved Permanently');
		header('Location: '.$url);
		exit();
	}

	if ( strpos($_SERVER['HTTP_HOST'], 'www.') === 0  && strpos($url, 'http://www.') === false ) {
		header('HTTP/1.1 301 Moved Permanently');
		header('Location: http://' . substr($_SERVER['HTTP_HOST'], 4) . $_SERVER['REQUEST_URI']);
		exit();
	} elseif ( strpos($_SERVER['HTTP_HOST'], 'www.') !== 0 && strpos($url, 'http://www.') === 0 ) {
		header('HTTP/1.1 301 Moved Permanently');
		header('Location: http://www.' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
		exit();
	}
?>

Option 2 – .htaccess URL rewrite
This option uses .htaccess to rewrite your website url. Many consider this a better option from a performance standpoint, but I can’t see enough of a difference to say if I agree.

301 Redirect to url with no www
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.yourwebsite.com$ [NC]
RewriteRule ^(.*)$ http://yourwebsite.com/$1 [R=301,L]

301 Redirect to url with www
RewriteEngine on

RewriteCond %{HTTP_HOST} ^yourwebsite.com$ [NC]
RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [R=301,L]