PHP URL rewrite-htacces

30th Jan 2010 at 1:01 PM | Posted in PHP, URL Rewriting | 4 Comments

By rewriting url we can change the original url. We can hide the extension of page. It is  also named as SEO friendly URL.

Before Start rewrite URL:

For rewrite the URL you must have the mod_rewrite module loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error. If you don’t know about mod_rewrite module then please check this post to know how to check and enable mod_rewrite module in apache?


1. Type <?php phpinfo(); ?> in a php file and save it and run that file in the server. Run tis page.

2. If mod_rewrite is found under the “Loaded Modules” section then this module is already loaded as you see in the picture above, otherwise you need to go to the next step for enabling mod_rewrite module.

If not active then active it:

1.  Find the “httpd.conf” file under the “conf” folder inside the apache’s installation folder.

2. Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.
3. Remove the “#” at the starting of the line, “#” rpresents that line is commented.
4. Now restart the apache server. You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.

Now a simple script:
1. Create a folder “url_rewrite”. Inside this we will create our files.
2. Create a file test.php:

<html>
<body>
This is test webpage
</body>
</html>

3. Create a .htaccess file. If you can’t create make a file create_htaccess.php.
<?php
$url=$_SERVER[‘REQUEST_URI’];
$url=explode(“/”,$url);
$final_url=”;
for($i=0;$i<(count($url)-1);$i++)
{
$final_url.=$url[$i].”/”;
}
if(!file_exists(“.htaccess”))
{
if($filename=fopen(“.htaccess”,”w”))
{
echo “.htaccess file created.<br/>Path: “.$final_url;
fwrite($filename,”Options +FollowSymLinks
RewriteEngine on”);
}
fclose($filename);
}
else
echo “.htaccess file already exists in: “.$final_url;

?>

4. In the .htaccess file write code  below:
Options +FollowSymLinks
RewriteEngine on

#give your base url path
RewriteBase /sourav/url_rewrite/

RewriteRule ^(.*)\.htm$ $1.php [nc]

[ Explain: In this we rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.]

5.     Now run the test.php file in browser or
run the test.html file in browser the result is same that is opening the test.php file.
Here we overwriting the extension .php by .html.

Now litle complex script:
1.Create a file “common_function.php” your url_rewrite folder.

<?php
function rewrite($val,$id)
{
if($val==”products”)
return “product-“.$id.”.html”;

if($val==”product_details”)
return “product_details-“.$id.”.html”;
}
?>
2. Create another file “products.php”.
<?php
echo “This is products.php?id=”.$_REQUEST[‘id’];
?>
3. Create another file “product_details.php”.
<?php
echo “This is product_details.php?product_id=”.$_REQUEST[‘id’];
?>
4. Include the “common_function.php” file in your test.php page.
<?php
include_once(“common_function.php”);
?>
This is test webpage<br/>
go to <a href=”<?php echo rewrite(“products”,”5″); ?>”>products.php?id=5</a>
go to <a href=”<?php echo rewrite(“product_details”,”3″); ?>”>product_details.php?product_id=3</a>

5. Now change your .htaccess file:
Options +FollowSymLinks
RewriteEngine on

#give your base url path
RewriteBase /sourav/url_rewrite/
RewriteRule ^(.*)\.htm$ $1.php [nc]
#This is for product page
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1

#This is for product_details page
RewriteRule ^product_details-([0-9]+)\.html$ product_details.php?id=$1

6. Now run test.php file or test.html file and click on the links on this page.
see the links are changed.

2. If it is found under the “Loaded Modules” section then this module is already loaded as you see in the picture                below, otherwise you need to go to the next step for enabling mod_rewrite module.

Blog at WordPress.com.
Entries and comments feeds.