Apache rewrite - some tips for newbies

October 24, 2007

It is important to have customized URLs, because search engines like Google indexes only certain URLs. Google “hate” symbols like “?” or “&” and so our website won’t be appreciated. So, it is a problem that should be fixed as soon as possible because that means a better ranking.

Look at the URL below:
http://www.example.com/index.php?pid=categories& action=delete&eid=3
We can change it this way:
http://www.example.com/categories/delete/3/
Much better, isn’t it? Besides it is easy to be kept and “appreciated” by Google or Yahoo! or whatever.

Apache and .htaccess

Apache, the most popular web-server over the web is very cool when it comes to features. It offers a way to solve the mentioned problem by URL Rewriting techniques. There is a way to tell the web-server to redirect a special formed HTTP request to another URL. For instance, we can decide to redirect everything like /?p=XXX to /p/XXX . The rules are achieved using the fantastic regular expressions. You can specify almost any pattern that could match everyone’s need. The rewrite rules are all present in the so called .htaccess. Nothing fancy here… It is just a file like any other; it’s just its name that is different but no problem; it could be any other.

Usually the .htaccess file is placed into the website root but this is not generally true. Basically it can be placed in any folder accessible via HTPP. This file is a txt one and can be edited with any text editor.We want to achieve the URLs like the above for the example presented on this page.http://www.example.com/categories/
http://www.example.com/categories/
delete/

We will start editing our .htacces file with the condition:

<IfModule mod_rewrite.c>RewriteEngine on</IfModule>

This conditions tests if the mod_rewrite module is enabled in Apache; this one takes care of rewriting stuff. If the answer is true, the rewrite engine will be enabled. In most hosting packages this is set ON, but it is wise to check this though.
Every page in the site will have $_GET parameters, starting from zero to a settled number. Many programmers like the way where the first parameter is the page, the second the action and the others vary.
Suppose we have a GET like:

$_GET = Array(page,action,param1,param2)

We will use the rewriteRule to rewrite the first parameter “page”:

RewriteRule <regex::result> <string::url> </string::url></regex::result>

<regex::result>
specifies the result we want to reach and <string::URL> is the initial URL with ? and &. So, if we will type in browser <regex::result>, the server will reply <string::URL>. Lets make things clear with an example.

Regular expression

RewriteRule ^([a-zA-Z]+)[/]*$ /?page=$1

Here we have a regular expression. If you are not familiar with this, I will tell you that may look difficult but it is not(If you want to learn more about regular expressions read our small regex tutorial). Through this expression we told the Apache to request /?page=pg when something like /pg/ is called into the browser.

And now, how we use GET?

This is a natural question. If our URL looks: http://www.example.com/categories/delete/3/ how we find the keys and values from GET. Remember that we used some standard keys: page, action, param1 and param2. These are the array’s GET keys, and the values are the characters between the slashes. So, being able to know these keys and looking at the URL we can easily deduct the GET:

$_GET = array (

'page' => 'categories','action' => 'delete',

'param1' => '3'

)

The server works with another URL, the one we talked from the beginning. The rewrite URL is practical used to looks friendly but the server act as usually.

 

Post a comment

Name (required)

Mail (will not be published) (required)

Website

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word