Archive for May, 2008

SEO Friendly URL Using MOD_REWRITE

Wednesday, May 28th, 2008

One aspect in SEO (search engine optimization) is having simple and descriptive URL. For example compare these two url:

  1. http://www.yoursite.com/product.php?id=101
  2. http://www.yoursite.com/product/laptop

The second URL is easier to remember for human / our visitors and it goes the same for Search Engine. Some search engine do not like special characters such as ‘?’,’=’ in URL.

Now Let me introduce you to MOD_REWRITE (rewrite module for Apache web server). This module is able to redirect a URL request to other page/URL. You can achieve the same behavior by using PHP redirect (header(“Location: destination.php”);), however, mod rewrite give you more control and robustness. What’s more, MOD_REWRITE redirect the user seamlessly which means the user will not know if they are redirected. I will go through the simplest steps to create SEO friendly URL using Apache MOD_REWRITE module. This blog is also serve as basic or simple guide on apache mod rewrite module.

Enough for the chitchat let’s go into main business. First of all you have to make sure that your apache server has mod_rewrite module enabled. By default this module is included in apache web server distribution but it is turned off. So go ahead, edit your server httpd.conf, remove ‘#’ in the Load Module section for rewrite_module or mod_rewrite.so, and restart your server.

All URL rewrite rules for mod rewrite should be put inside “.htaccess” file. Please note the behavior of htaccess file in apache which will apply the rule recursively, thus if you put your “.htaccess” in your web root folder it will affect all other folder under this root folder. Create a “.htaccess” file with the following content:

RewriteEngine on
RewriteRule ^(.*)\.html displayPage.php?page=$1

Explanation of above rule:

  • RewriteEngine on will activate any mod rewrite rule that we have specified, you can put “RewriteEngine off” to turn off rewrite rule on certain folders.
  • RewriteRule ^(.*)\.html displayPage.php?page=$1
    This rule will rewrite the url which ended with “.html” so that it will go to displayPage.php. So if you try to go to http://www.mysite.com/our_products.html, you will be redirected to http://www.mysite.com/displayPage.php?page=our_products.

    1. ^(.*)\.html – is a regular expression, any URL that match this expression will be redirected. Please note that we can capture any characters that match regular expression by using brackets “( )”. We can refer to this captured string for redirection purposes by using $n (n = number 1,2,3,4…). For more info on regular expression go to http://www.regular-expressions.info/reference.html
    2. displayPage.php?page=$1 – is the redirection destination. $1 is variable which will contain any character that has been captured before, in our regular expression.

Now create “displaPage.php”, inside this page you can get the page parameter and display the actual page accordingly. For example:

<?php
    $requestedPage = $_GET['page'];
if ($requestedPage == 'our_products') include("page1.php");
else include("index.php");
?>

Now, If you try to access “our_products.html” in your server you will be viewing “page1.php”. You can do many things inside the displayPage.php in this simple example you may find it is not quite useful, but imagine if you rewrite product name in the url (i.e. fender_electric_guitar.html) into their product code / category code (product.php?productId=50541). In order to achieve this, you can query your database and look for the product id for any product name, inside displayPage.php. Hope you find this blog helpful.

Setup Zen-cart for non-English Language

Monday, May 5th, 2008

Within the last couple of days, I have been stressfully tried to upgrade Zencart shop with French language. I have backup the same data in the old database exactly, and move it to my workstation. I have installed French language pack for zen-cart, but it does not work. The special characters in French / other languages (i.e. é, à, ç and the like) was rendered differently (become ê, é, etc.).

At first, I thought the problem was on the database. Since I am not allowed to upload any new files to the server and I don’t want to create security risk by uploading phpmyadmin, so I used MySQL Administration tools to process the database. I have tried to use backup function and MySQL Migration tool but the characters on the web page is still faulty. I have also tried to change the collation and database type from “latin” to “utf8”, but again it did not work.

Finally, I just figured out that it has nothing to do with the database. I just have to change the CHARSET of the html page of the zencart, which can be modified from the language file (English.php, French.php, etc.). The strange characters was rendered differently because It was rendered using iso-8859-1 not UTF-8. The solution is to modify the following line “define(‘CHARSET’, ‘iso-8859-1’);” into “define(‘CHARSET’, ‘utf-8’);”.