The Most Active and Friendliest
Affiliate Marketing Community Online!

“AdsEmpire”/  Direct Affiliate

Making Search Engine Friendly URL

M

midlandi

Guest
Making Search Engine Friendly URL


An htaccess file is a simple ASCII file. .htaccess is the file extension. It is not filename.htaccess or anyfile.htaccess. It is simply named .htaccess. This file has many usage including password protection, enabling SSI, deny users by IP, changing your default directory page, making search engine friendly URLs, redirects, adding MIME types and many more.

Search Engine Friendly URL
Search engines like Google are much better at indexing static pages rather than hyperlinks that contain query strings. The robots used by search engines have problems with dynamic pages. However, that dynamic hyperlinks can be changed to static one allowing them to crawl all the pages. This approach is good especially for search engine promotion and site ranking. For example:

http://www.mysite.com/view_computer.php?key=1

can be easily rewritten as

http://www.mysite.com/computer_1.html

thus eliminating the "?" sign in the URL.

The example shown above referred to a module called mod_rewrite. This is a module compiled into Apache web server. This will be the usage of .htaccess file we talked earlier. mod_rewrite makes use of the .htaccess to perform rewrites which in turn tells the server how to convert between static and dynamic URLs.

Check your server
Before we can create .htaccess file, we'll make sure that the server where our script running support the mod_rewrite module. If you have a web host, consult the technical support. If the support is not good (probably not coz most host offer 24/7), you can check it by viewing the phpinfo(). To do this, create a php file:

phpinfo();
?>

save it as info.php, upload it to your server, then on your browser type http://www.mysite.com/info.php. It will display lots of information, just look for the heading "apache" and then "Loaded Modules" and you must see mod_rewrite there and proceed to creating the file. If you're just running th scripts in your local machine, open the Apache configuration file - httpd.conf file and find the following line:

LoadModule rewrite_module

Make sure that it has no comment sign (#) before it.

Creating .htaccess
Now we're ready to create .htaccess. I recommend using text editor like Notepad to do this one. I'm assuming that the URL we are going to change is

http://www.mysite.com/view_computer.php?key=1

to

http://www.mysite.com/computer_1.html

Here's the code for .htaccess:

RewriteEngine On
RewriteRule ^computer_([a-zA-Z0-9]+)\.html$ view_computer.php?key=$1

Ok let's break it down. The first and second line is just a comment so no need to go through with it. Next I am forcing Apache to turn on the rewrite engine for the URL's using the RewriteEngine On.

* The caret (^) stands for "anything before" in the URL.
* The (computer_) will be the part of the new URL where the users redirected to.
* ([a-zA-Z0-9]+) is a variable as defined by the brakets. The conditions inside the brakets say that the variable can have small or uppercase letters and can have numbers only. The plus simply means it has no set limit to the number of characters or numbers. This allows for maximum compatability and expandability.
* The slash before the full stop, \, is used to make sure Apache understands that the full stop is a normal character and isn't being used as or with another function.
* .html will be the extension of the new URL.
* view_snippet.php is the real php page that we target and be replaced with the new one.
* key=$1 means that the first variable, the key defining one, is echoed in place of $1.
* Upload it to your server and job done!

Put in Action
You may be asking, where I'm going to put this or that. Let's say you have a database of computers and you have a page, say computerlist.php, that list all of it by name and each name has a link to view the detailed part of the computers listed. It may have the following code:

// Some stuff of queries here..
while($rows = mysql_fetch_array($query))
{
$key = $rows['ComputerId'];
$name = $rows['ComputerName'];

echo "" . $name . "";
}
?>

Change "" . $name . ""; to "" . $name . ""; making your final code look like this:

// Some stuff of queries here..
while($rows = mysql_fetch_array($query))
{
$key = $rows['ComputerId'];
$name = $rows['ComputerName'];

echo "" . $name . "";
}
?>

Don't stop right here, play with the .htaccess and you can have more complex dynamic URLs to change into a simple search engine friendly.

Source URL: http://www.triphp.com

About The Author
Ramil Alcibar is a contributor to TriPHP Webmaster Resources.
 
banners
Back