Have you seen those websites such as Digg and SVP that use fancy URLs such as http://domain.com/page_content? Yeah, sure this could be nearly achieved by simply renaming your pages to something like page_content.htm - but that would result in you having to change every page on your site to a suitably written filename, as well as relying on static pages. I'm going to attempt to demonstrate just how easy it really is to utilise mod_rewrite to have fancy - even "web 2.0" - URLs within your site.
Mod_rewrite is useful for pages with dynamic content, such as those which rely on either POST or GET data to generate the content. A URL that looks something like
http://domain.com/product.php?product_id=19
would be ideal to control using mod_rewrite. The main reasons being that:
So let's get started shall we? If you know mod_rewrite works, start masking pages with mod_rewrite now!.
In order to use mod_rewrite, you will need the following:
Don't panic - most Apache web servers have the above enabled and ready to go! Let's do a simple test to make sure everything is cool to go. Create a .htaccess file in your site's root directory.
Insert the following text into your new .htaccess file:
NOTE: The .htaccess file will affect all files within the same directory as itself, as well as sub-directories and their respective files. The directory can change, but for the purposes of this tutorial, we're gonna stick this bad boy in your root directory.
Upload your .htaccess file to the root directory of your site, and load the index page (well, any valid page for that matter!). One of two things will happen (at least, I think I'm right here…). You'll find that either:
If you get the Internal Server Error (error 500), I'm afraid that it looks like either mod_rewrite isn't installed, or your httpd.conf file doesn't allow the required AllowOverride directives. If this is the case, and you do have access to edit your httpd.conf file, do so and set AllowOverride to ALL for your site root directory:
NOTE: You can also add all of the mod_rewrite rules within the httpd.conf file instead of the .htaccess file if you so wish. My preference is to use the .htaccess file as it's more portable, and generally easier to edit.
After you've edited the httpd.conf file / contacted your server guys or whatever - you should be good to go. If not, I'm all out of ideas. Sorry. Try Google for help :].
Assuming all's working well, let's get to the cool bits!
This is kind of pointless, but is a simple demonstration for this tutorial. If you don't care much for pointless, simple-to-understand demonstrations, go straight to the useful rewrite rules.
First create two basic HTML pages. Name them 'page_one.htm' and 'page_two.htm'. Add a little dummy content to both of them, but make sure they have different content (this is so as we can differentiate the pages). Open your .htaccess file, and if you don't have the main opening lines:
Add them now. On the next free line, add the following:
Now, load page one. What do you see? Page two hopefully. How? Well, the RewriteRule breaks down like this:
RewriteRules have 4 main parts to them. The rewrite action:
Then the FIND THIS part:
This is a regular expression. Everything found in between the circumflex (^) and dollar sign ($) will be searched for and saved as a variable (more on this later). If the FIND THIS value is found, it'll be replaced with the third part of the RewriteRule - the REPLACE WITH THIS:
The fourth and final part of the RewriteRule is the flags. You can put flags in between square brackets ([ ]) to control how mod_rewrite should deal with the rule itself.
I've simply used one flag, 'L'. This means that this is the last rule. As we only have one rule at the moment, using this flag seems appropriate, as it's the only, and last, rule. You can use multiple flags, by comma delimiting them. There is a list of the flags at the end of this tutorial.
Let's take the first URL mentioned in this tutorial as the example for this part of the tutorial:
http://domain.com/product.php?product_id=19
Create a new PHP page called product.php. Add the following code between the
tags:Save, and upload your file. You can view my example of product.php to see how the page changes with the dynamic content from the GET request.
Let's say you would prefer to use a cleaner URL, such as:
http://domain.com/product/19/
This could be achieved by adding the following line to your .htaccess file:
You can test this in action if you wish. If we break down the RewritRule into the four parts again, you should be able to see how it's working:
Here, we're simply stating that we're adding a new rule for mod_rewrite to use.
As before, this is the FIND THIS value. We are looking for a string within the URL that contains product/[NUMERICAL VALUE]/ - that is, the word 'product', then a forward-slash (/), then a numerical value only, then a final forward slash. The magic here lies between the brackets ( ):
The brackets ( ) are telling mod_rewrite to store whatever it finds within the brackets in a variable. The [0-9]+ is a simple regular expression allowing only numbers 0 to 9, and the + sign means 'any number of'. So we can have 99 or 123 for example, not just one number, such as a 9 or a 1.
we finally add a trailing slash (/) at the end to finish what we're looking for (as our desired clean URL is product/[PRODUCT ID]/).
The third part of our RewriteRule is the REPLACE WITH THIS part. Here you can see the actual page name, with the query string - but this time there's something new. Where we used to have the product ID, we now have a variable $1. $1 will contain the value of the result of the regular expression found within the brackets in the FIND THIS part. Can you see it all falling into place now?
Finally come the flags. I've used two flags here, NC meaning that the rule is case insensitive (it just protects us incase anyone uses http://domain.com/Product/19/ for example), and L meaning it's our last rule.
Feel free to check this out with a working example. Change the number from 19 to a different number and see what happens.
I could go on and on with more examples of mod_rewrite, but that would exceed the scope of this tutorial - seeing as this is only a beginner's tutorial. Through this tutorial, I hope that you've learned enough of the basics in order to get you started. You should now know:
I followed your example excatley, however it just doesnt work. Ive been trying for a couple of hours to get any SEO friendly .htaccess scripts to work.
I know rewrite is enable, as i can do a simple x.php to b.html easy enough.
But for some reason your code just does nothing? Any ideas?
Many thanks in advance
Hi James,
Could you post your .htaccess script and related .php file code? Alternately send them to me and I'll happily look at them for you.
Cheers!
Aloha.. thx for that tutorial..
For some reasons this wont work for my site :S
i tried following:
RewriteRule ^clip/([0-9]+)/$ index.php?action=goto&url=$1 [NC,L]
Pleez help
Hi FX,
Did you do the Testing Mod_Rewrite Works section, and can confirm that it works ok? If so could you send me your full .htaccess file, and point me to where you're hosting it?
Use the contact me link at the top of this site to send me any files for checking.
Hey,
This is great, thanks for the post.
Just wanted to confirm that it works for me, I've noticed that some people have said they're having problems. Make sure that you have mod_rewrite installed, and go through the Testing part of this tutorial to check it all works.
c0de.
Your tutorial was helpful! I have one question on url masking
I would like to restrict the url displayed in the address bar of the browser to main page say http://www.mysite.com/welcome
now even if user moves to another link say page2 I want the display to stick to http://www.mysite.com/welcome instead of
http://www.mysite.com/welcome/page2
I would appreciate if you could give me some pointers or share an example code demonstrating how this can be done.
Hi Sukesh,
I have added a live demo of the URL masking guide at:
http://www.edrackham.com/tutorials/beginners_mod_rewrite_tutorial/test/page_one.htm
Is this what you're looking for? It's using the same code in the URL masking section of this tutorial. When you load the above link, you will see page two, but the URL remains as page one.
Dear Ed,
maybe you know where to find information how to step by step make ssl server from Apache side and PHP.
view video clips free fucking