Translator
Categories
Archives

Archive for the ‘PHP’ Category

Improve the SEO of your website in one step

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]

Connecting to MySQL with PHP

You can use the following code to connect to a MySQL database with PHP.

<?php
$host = "localhost"; //Address or IP of MySQL Database
$username="username"; //Username for MySQL Database
$password="password"; //Password for MySQL Database
$database="dbName"; //MySQL Database Name
mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Failed to select database");
?>

Get Files and Folders with PHP

If you need to get Files and Folders using PHP you can use the following code.

$fileArray = array();
$dir = $_SERVER["DOCUMENT_ROOT"]; //The folder to search
if ($folder = opendir($dir)) {
	while(($file = readdir($folder)) !== false) {
		if ($file != "." && $file != "..") {
			$fileArray[] = $file;
		}
	}
	closedir($folder);
}

foreach ($fileArray as $file) {
	//Do something to each file.
}

PHP Automatic Link Function

If you want to automatically turn any URL on a page into a link, you can use the following PHP function.

function makeLink($source)
{
   $result = eregi_replace("(http|https|ftp)://([a-z0-9\-\./]+))","\\0",$source);
   return $result;
}

Basic MySQL / PHP Select Query

The SELECT statement is used to pull a desired collection of data from a database.

$query = mysql_query("SELECT * FROM 'table' WHERE 'column'='value'");
while ($result = mysql_fetch_array($query)) {
	$id = $result["id"];
}