Warning: session_save_path() [function.session-save-path]: open_basedir restriction in effect. File(/var/www/vhosts/4mysql.com/httpdocs/cache) is not within the allowed path(s): (/home/mysqlc4:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/mysqlc4/public_html/includes/functions/sessions.php on line 145
News, Tips & Ideas : MySQL Databases For Sale, SQL Files, CVS Files, SQL Format, CVS Format
MySQL Databases & PHP Software

TRANSLATE:

Mod-Rewrite For SE Friendly URLs - A Practical Example

Published: Saturday 06 January, 2007

We get a lot of questions about how to use Mod-Rewrite on their Unix-Platform web site, so they can eliminate query expressions in their url and make it Search Engine Friendly. (From query.php?keywords=mod-rewrite to mod-rewrite.htm)

This will be a quick lesson with a script we use all the time. There are also some more advanced Mod-Rewrite examples that we will present in our upcoming eBook entitled Using Mod-Rewrite For Search Engine Friendly URLs.

But for now, let's keep it simple.

Most applications that use our databases use a simple query format: query.php?keywords=something here

This simple query is presented in the mod-rewrite code as query.php?keywords=$1

So to begin, be sure to always add these two lines in your .htaccess file:

Options +FollowSymLinks
RewriteEngine On

Your next line will take the URL

  
[yourdomain.com/mod-rewrite.htm]  

and rewrites it on the Server Side to 

   
[yourdomain.com/ query.php?keywords=mod-rewrite]

The entire code, which is placed into your .htaccess file is:

Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*).htm$ query.php?keywords=$1

There are many variables available in Mod-Rewrite, but this should give you a basic format to begin with.

By having this code in your .htaccess file - you can now use SE Friendly URLs. So 'yourdomain.com/roses.htm' and 'yourdomain.com/furniture polish.htm' will be converted on your server and deliver content from your database about 'roses' and 'furniture polish'.

And to give you something a little more advanced, we will leave you with the following mod-rewrite code. Understanding the above information should give you a good base of understanding from which to understand the following:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$1+$2+$3+$4+$5 
RewriteRule ([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$1+$2+$3+$4
RewriteRule ([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$1+$2+$3
RewriteRule ([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$1+$2 
RewriteRule ([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$1
RewriteRule ([a-zA-Z]+)/$ search.shtml?keywords=$1
RewriteRule ([a-zA-Z?0-9]+)/([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$2+$3+$4+$5 
RewriteRule ([a-zA-Z?0-9]+)/([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$2+$3+$4
RewriteRule ([a-zA-Z?0-9]+)/([a-zA-Z?0-9]+)-([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$2+$3
RewriteRule ([a-zA-Z?0-9]+)/([a-zA-Z?0-9]+)\.htm$ search.shtml?keywords=$2

Back
Your IP Address is: 38.107.191.95
Copyright © 1993-2007 4 MySQL