Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a site developed in php and now I am almost at the end of launching it but just want to make the URL user-friendly. My site is dynamic and in links which is dynamically created has query string in URL so now how can I make it user-friendly URL.

For example my url which I give in "a" tag like http://www.example.com/product.php?product_name=abc_pqr&product_id=452

then how can I make it as

http://www/example.com/product/abc_pqr/452

I know it can be done using .htaccess file but I am very new for URL Rewriting and like a newbie to it.
Posted

1 solution

You can make the mod_rewrite module load dynamically in to the Apache web server environment using the LoadModule Directive in the httpd.conf file. Load this file in a text editor and find a line similar to the one given below.

#LoadModule rewrite_module modules/mod_rewrite.so

Uncomment this line by removing the # and save the httpd.conf file. Restart your Apache server and if all went well mod_rewrite module will now be enabled on your web server.

Lets Rewrite our first URL using mod_rewrite

Ok, now the mod_rewrite module is enabled on your server. Lets have a look at how to make this module load itself and to make it work for us.

In order to load the module dynamically you have to add a single line to your .htaccess file. The .htaccess files are configuration files with Apache directives defined in them and they provide distributed directory level configuration for a website. Create a .htaccess file in your web servers test directory - or any other directory on which you want to make URL Rewriting active - and add the below given line to it.

RewriteEngine on

Now we have the rewrite engine turned on and Apache is ready to rewrite URLs for you. Lets look at a sample rewrite instruction for making a request to our server for first.html redirected to second.html at server level. Add the below given line to your .htaccess file along with the RewriteEngine directive that we have added before.

RewriteRule ^first.html$ second.html

I will explain what we have done here at the next section, but if all went well then any requests for first.html made on your server will be transferred to second.html. This is one of the simplest forms of URL Rewritting.

A point to note here is that the redirect is kept totally hidden from client and this differs from the classic HTTP Redirects. The client or the browser is given the impression that the content of the second.html is being fetched from first.html. This enables websites to generate on the fly URLs with out the clients awareness and is what makes URL Rewriting very powerful.

Basics of mod_rewrite module

Now we know that mod_rewrite can be enabled for an entire website or a specific directory by using .htaccess file and have done a basic rewrite directive in the previous example. Here I will explain what exactly have we done in the first sample rewrite.

Mod_rewrite module provides a set of configuration directive statements for URL Rewriting and the RewriteRule directive - that we saw in the previous sample - is the most important one. The mod_rewrite engine uses pattern-matching substitutions for making the translations and this means a good grasp of Regular Expressions can help you a lot.

Note: Regular Expressions are so vast that they will not fit in to the scope of this article. I will try to write another article on that topic someday.

1. The RewriteRule Directive

The general syntax of the RewriteRule is very straightforward. RewriteRule Pattern Substitution [Flags]

The Pattern part is the pattern which the rewrite engine will look for in the incoming URL to catch. So in our first sample ^first.html$ is the Pattern. The pattern is written as a regular expression.

The Substitution is the replacement or translation that is to be done on the caught pattern in the URL. In our sample second.html is the Substitution part.

Flags are optional and they make the rewrite engine to do certain other tasks apart from just doing the substitution on the URL string. The flags if present are defined with in square brackets and should be separated by commas.

Lets take a look at a more complex rewrite rule. Take a look at the following URL.

http://yourwebsite/articles.php?category=stamps&id=122

Now we will convert the above URL in to a search engine and user friendly URL like the one given below.

http://yourwebsite/articles/stamps/122

Create a page called articles.php with the following code:

$category = $_GET['category'];
$id = $_GET['id'];
echo "Category : " . $category . " ";
echo "ID : " . $id;
This page simply prints the two GET variables passed to it on the webpage.

Open the .htaccess file and write in the below given Rule.

RewriteEngine on
RewriteRule ^articles/(\w+)/([0-9]+)$ /articles.php?category=$1&id=$2

The pattern ^articles/(\w+)/([0-9]+)$ can be bisected as:

^articles/ - checks if the request starts with 'articles/'

(\w+)/ - checks if this part is a single word followed by a forward slash. The parenthesis is used for extracting the parameter values, which we need for replacing in the actual query string, in the substituted URL. The pattern, which is placed in parenthesis will be stored in a special variable which can be back-referenced in the substitution part using variables like $1, $2 so on for each pair of parenthesis.

([0-9]+)$ - this checks for digits at the last part of the url.

Try requesting the articles.php file in your test server with the below given url.

http://yourwebsite/articles/coins/1222

The URL Rewrite rule you have written will kick in and you will be seeing the result as if the url requested where:

http://yourwebsite/articles.php?category=coins&id=1222

Now you can work on this sample to build more and more complex URL Rewritting rules. By using URL rewriting in the above example we have achieved a search engine and user friendly URL, which is also tamper proof against casual script kiddie injection sort of attacks.

What does the Flags parameter of RewriteRule directive do?

RewriteRule flags provide us with a way to control the way mod_rewrite handles each rule. These flags are defined inside a common set of square brackets separated by commas and there are about 15 flags to choose from. These flags range from those which controls the way rules are interpreted to complex one's like those which sent specific HTTP headers back to the client when a match is found on the pattern.
 
Share this answer
 
Comments
bhavikadb 9-Oct-15 5:20am    
Thanks alot for your brief description and a small tutorial. Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900