- Forums
- Tutorial and Guides
- Explain How MOD REWRITE Works
This Page Contains information about Explain How MOD REWRITE Works By Webune Tutorials in category Tutorial and Guides with 0 Replies. [740], Last Updated: Mon Jun 24, 2024
Webune Tutorials
Sun Nov 08, 2009
0 Comments
428 Visits
Hello, Welcome to Webune Support Forums - Today we are going to try to explai a little bit about mod rewrite. many of our shared hosting customer are asking if we provide this feature in our PHP shared hosting servers. The answer is YES. Webune offers web hosting with mod rewrite already enabled.
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=23as 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.htmlor
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.htmlok, 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]
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