URL Rewriting Using htaccess file and PHP Apache

Hello friends,

From long time I was wondering how this URL rewriting is working. I have seen many website with pretty URLs. Also I found the URL Rewriting also helps in Better SEO (Search Engine Optimization).

After Reading bunch of tutorial and forums, I think this is very easy to make just few steps to follow and need knowledge of regular expression.

Steps for Implementing URL rewrite in you website.

  1. Enable mod_rewrite in Apache Server
  2. Create Htaccess file.
  3. Call the URL with New URL Defined.

Enabling mod_rewrite in Apache Server

For enabling mod_rewrite on Apache Server follow these steps:

  1. Find http.conf file in Apache server. Generally this file resides in conf folder of Apache installation PATH. XAMPP server path is C:\xampp\apache\conf on windows. Probably WAMP Path is C:\wamp\apache\conf.
  2. Find the line #LoadModule rewrite_module modules/mod_rewrite.so in the “httpd.conf” file. You can do this easily by searching the keyword “mod_rewrite” from find menu.
  3. Uncomment the line. http.conf file use # to comment the line. So remove the # from begining of the line.
  4. Restart the server. In WAMP you can restart the server by click the icon of WAMP in system tray in right bottom side of the windows. For XAMPP you can open XAMPP control and restart the server.
  5. You can check the mod_rewrite is enabled using phpinfo();

Create HTACCESS file

Lets consider one real world example for creating and understading HTACCESS file and its rules. HTACCESS file uses RewriteRule for define the rule for URL rewriting. This will show you good example for regular expression, rewrite rules & conditions.

For example, create file testhtaccess.php. Write

"This is PHP HTACCESS Test File"

Create second file testhtaccess.html and Write

"This is HTML HTACCESS Test File".

This is simple text written in the both the files so that we can differentiate when we rewrite the URL which file is called.

Create Third file .htaccess in same folder of the server where these two files are and Write

RewriteEngine On
RewriteRule ^/?testhtaccess\.html$ testaccess.php [L]

The first line turns RewiteEnging On. It will allow the server to rewrite the URL. Second line is the rule for URL rewriting. “^/” says that from starting any type of character and any number of character are allowed. Then there is “?testhtaccess\.html”  \ is for escaping the . in name and there is $ which implies that this is end for rule. This is first Parameter for RewriteRule. Now second parameter tells which files to call when url is matched.

So here when we call testaccess.html file is will be rewite the url and call testhtaccess.php file. This will done internally and URL remains the same.

Lets go now little complex for this. lets say that we have URL and Query String like as below:

http://mywebsite.com/details.php?city=cityname&company=companyname

we need to rewrite is like

http://mywebsite.com/cityname/companyname

Now we have to tell the apache that whenever this second rewrited url called it should internally call first URL with query string. Now for the ditails.php script to read and parse the query string, we’ll need to use regular expressions to tell mod_rewrite how to match the two URIs. For this you should have knowledge of regular expression. Even though I am going to explain some key factor of regular expression.

. (dot) – Matches any Single Character eg. c.t will match cat, cut, cot

+ (Plus) – repeates the previous match one more time

* (asterik) – repeats the previous match zero or mote times

? (question mark) – Makes the match optional

^ (anchor) – matches beginning of the string

$ (anchor) – matches end of the string

( ) – group several character into single unit

[ ] -a character class – matches one of the characters. it used for range of the character.

 [^ ] – negative character class – matches any character not specified.

Note: ! character can be used before a regular  expression to negate it.

Now for our example we can use

^([a-zA-Z_]+)/([a-zA-Z_]+)$

or

(.*)/(.*)

for replace Rewrite URL with Actual URL. Using ( ) we can make a variable which can be used on later. This Variable can accessed by

$1

Value incremented automatically on the sequence of occurring of the ( ).

RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+))$ details.php?city=$1&company=$2 [L]

This will replace the Rewrite URL with Actual One.  [a-zA-Z_]+ will match any string in lowercase or Uppercase. Its in small bracket ( ) so that it store its value in $1 and when it occurs second time it will store its value in $2.

This is so simple as we can see. I have written this post for the beginners like me. I written what I learn reading other forums and blog which are seems to be very complex for understanding it.

Any Correction and advice from you friends will be appreciated.

Thank you Please comment here if any questions.. 🙂