Explain How MOD REWRITE Works
RegisterLogin
Explain How MOD REWRITE Works
Post Description: Explain How Mor Rewrite Works
Tags: Explain, How, Mor, Rewrite, Works
This Post Was Posted On Nov 08, 2009 By Webune Tutorials #2701
Post Description: Explain How Mor Rewrite Works
Tags: Explain, How, Mor, Rewrite, Works
This Post Was Posted On Nov 08, 2009 By Webune Tutorials #2701
Explain How MOD REWRITE Works by Webune Tutorials
What is mod_rewrite?
Mod_rewrite is a module that comes in the Apache Server. Like many modules, rewrite is simple re-writing the url on your website.
for example, lets say i have a website and i have a PHP file called: category.php
each category has a unique identifier, for example, the catid. so to access each category from the url you can have the url that looks like this:
http://www.example.com/category.php?catid=23
as you can see from the example url above, its abvious that you are using php for your scripts. but how about if you can create a url that looks like this:
http://www.example.com/category-23.html
or
http://www.example.com/category/23/
can can use either setup, as long as you tell your server how you want to use your url in the .htaccess file
so to make things simple for us, we are going to choose this format:
http://www.example.com/category-23.html
ok, now that we have a plan of what we want, its time to create the files. open your favorite text editor.. i am using a windows computer so i will be using notepad for this tutorial
1. open a blank notepad and copy and paste this php code:
PHP CODE: [category.php]
<?php /* COPYRIGHT Webune.com */echo '<h1>The Category is: '.$_GET['catid'].'</h1>';function ModRewriteUrl($id){return $id.'.html';}echo '<table border="0" cellpadding="5"><tr><td>links uring mod rewrite: </td><td><a href="'.ModRewriteUrl(34).'">34</a> <a href="'.ModRewriteUrl(43).'">43</a> <a href="'.ModRewriteUrl(2345).'">2345</a></td></tr><tr><td>link NOT using mod rewrite </td><td><a href="'.$_SERVER['PHP_SELF'].'?catid=34">34</a> <a href="'.$_SERVER['PHP_SELF'].'?catid=43">43</a> <a href="'.$_SERVER['PHP_SELF'].'?catid=2345">2345</a></td></tr></table><br><a href="http://www.webune.com/forums">Learn More Tutotials</a><hr><img src="http://www.webune.com/images/headers/default_logo.jpg"><br><a href="http://www.webune.com/">Visit Webune.com for more TUTORIALS !!!</a>';?>
2. after you have copy and paste, save the file as: "category.php"
[NOTE: be sure to use the quotes or else notepad might save the file as category.php.txt - we dont need the .txt]
3. now open another blank notepad to create the htacccess file and copy this code:
RewriteEngine On
RewriteRule ^([^/]*).html$ category.php?catid=$1 [L]
RewriteRule ^([^/]*).html$ category.php?catid=$1 [L]
4. after you have put the code above, save it as ".htaccess"
[NOTE: be sure to use the quotes or else notepad might save the file as htaccess.txt - we dont need the .txt]
5. now create a test directory on your wesite and upload the two file you just create to that directory and test it yourself, for example:
http://www.example.com/test/category.php
Leave Your Comments
Related Pages: [Add Your Website]
Post New Topic
©2012 Webune Forums - Mon Dec 17, 2012 6:58 am
Powered by: Webune Forums V3
Powered by: Webune Forums V3