Apache Mod_Rewrite module is a really handy and cool feature in Apache Web Server.
With mod_rewrite, you can show a different url than the actual url. you will also help your website make it more search engine friendly by avoiding having the ? (question mark) in your urls.
for example, this topic i am writing right now here at Wallpaperama, im actually using mod_rewrite, if you look at the url in your browser, its says:
http://www.wallpaperama.com/forums/how-to-test-check-if-mod-rewrite-is-enabled-t40.html
but in reality, i am mascarading the actual php url. instead of using this url:
http://www.wallpaperama.com/forums/viewtopic.php?t=40
ok, the best way to learn is to actually do it yourself. if you have your website with a web hosting company that has mod_rewrite already enabled, then you shoulnnt have any problems. Webune.com customers benefit from apache's mod_rewrite. Every customer being hosted by our shared services has automatically mod_rewrite enabled.
You can confirm if its enable by following this simple tutorial guide. In this tutorial, you will be creating two files, the first one called rewrite.php and the second one .htaccess
- I will be using Windows XP notepad to create my files. ok, lets go..
step 1. to keep this organized, create a new directory (folder) called test
step 2. Open a blank notepad and copy and paste the following code into it:
rewrite.php PHP CODE:
<h2 align=center>
<?php
// mod_rewrite Test Page
// Copyright 2006 Webune.com
if($_GET['link']==1){echo"You are not using mod_rewrite";}
elseif($_GET['link']==2){echo"Congratulations!! You are using Apache mod_rewrite";}
else{echo"Linux Apache mod_rewrte Test Tutorial";}
?>
</h2>
<hr>
<head>
<title>How To Test mod_rewrite in Apache Linux Server</title>
</head>
<body>
<p align="center">by <a href="http://www.webune.com">Webune</a></p>
<p><a href="rewrite.php?link=1">LINK1</a> = rewrite.php?link=1</p>
<p><a href="link2.html">LINK2</a> = link2.html</p>
<p>How this works: both links are for this same page, except they both are different. link one is without the mod_rewrite and link2 is using mod_rewrite. Link1 show the php file, with with mod_rewrite we are mascarading the php file into a html file. you can use whatever type of extension you want, you can change it to .htm or .shtml etc... all you have to do is to make sure you also chang it in the .htaccess file</p>
<p><< <a href="http://www.webune.com/forums/viewtopic-p-62.html">Go back to webune forums.</a></p>
</body>
</html>
<?php
// mod_rewrite Test Page
// Copyright 2006 Webune.com
if($_GET['link']==1){echo"You are not using mod_rewrite";}
elseif($_GET['link']==2){echo"Congratulations!! You are using Apache mod_rewrite";}
else{echo"Linux Apache mod_rewrte Test Tutorial";}
?>
</h2>
<hr>
<head>
<title>How To Test mod_rewrite in Apache Linux Server</title>
</head>
<body>
<p align="center">by <a href="http://www.webune.com">Webune</a></p>
<p><a href="rewrite.php?link=1">LINK1</a> = rewrite.php?link=1</p>
<p><a href="link2.html">LINK2</a> = link2.html</p>
<p>How this works: both links are for this same page, except they both are different. link one is without the mod_rewrite and link2 is using mod_rewrite. Link1 show the php file, with with mod_rewrite we are mascarading the php file into a html file. you can use whatever type of extension you want, you can change it to .htm or .shtml etc... all you have to do is to make sure you also chang it in the .htaccess file</p>
<p><< <a href="http://www.webune.com/forums/viewtopic-p-62.html">Go back to webune forums.</a></p>
</body>
</html>
step 3. now save the file as rewrite.php in your test directory
step 4. Open a new blank notepad, we are going to create a file called .htaccess
step 5. Copy and paste the following code:
.htaccess File:
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
step 6. Save as .htaccess (if you are using notepad as I am, make sure to put quotes in between. example: ".htaccess" otherwise, notepad will create a file called .htacces.txt and we don't need the .txt at the end only .htaccess)
step 7. You should have two files in your test directory: rewrite.php and .htaccess - Now upload the test directory with the two files rewrite.php and .htaccess into your website.
step 8. Open the rewrite.php file with your browser to the location you just uploaded the files. for example: http://www.yoursite.com/test/rewrite.php
step 9. you should see two links, LINK1 and LINK2 Click on LINK1 and the same page will display.
step 10. Now click on LINK2 and the same page will display with the a mod_rewrite message. If it doesn't you either did something wrong. Check your steps. If you checked your steps, refresh your browser, and if refreshing doesn't work, then you don't have mod_rewrite enabled. if you dont have mod rewrite enabled in your server, then you might see something like this:
If mod_rewrite is not working for you and you are a Webune customer, please contact our support team to assist you.
DEMO:
Click on this link. We have provided uploaded the files in this tutorial to show you how mod_rewrite works.
mod rewrite tutorial
if you need more help, i provided some other links to other posts on this subject. hope this helps.
thanks.
HELP
Here are some links to help you incase you are having trouble or its not working for you see related linksplease provide any comments, i would like to learn from other by telling me why you need to learn mod rewrite so i can make more of these tutorials.