The basics of Mod_Rewrite
Friendly URL’s are nice. Nobody really likes copying or trying to type urls likeIt looks better; it’s search engine friendly, and it’s pretty simple to do on an apache webserver! Rewriting your urls also hides the underlying technology which powers your site; which is good for some, and the links look much more permenant.
http://yoursite.com/folder/do.php?do=rk2&cat=3
When you could just have
http://yoursite.com/folder/do/3/rk2
First Steps
ReWriting is done using the .htaccess file in your home or root directory, normally /var/www or public_html ; depending on your setup. You should make the file on your local computer as htaccess.txt, then upload it and change it’s name, because it has a tendancy to vanish once it’s uploaded, so you can’t download it again.Now, you need to activate mod_rewrite for your website, as in some servers it isn’t enabled by default. Provided it’s installed with Apache, add the following statement to the top of your .htaccess file, before any rewrite rules, and you’ll be fine.RewriteEngine OnNow, provided that file is in your root directory, mod_rewrite will be enabled.
Basics
We will begin by rewriting one static url to another; for demonstration purposes. We are going to visit page1.html and the webserver is going to show page2.html transparently.This is the simplest example there is, but it may throw some people off; so I will explain it:* The Caret (^) signifies the start of the URL to be matched, under the current directory (the one that the .htaccess file is present in)* The $ signifies the end of the string to be matched by Apache; after it comes the new string.* The backslash before the . has a special purpose, too. The . or full stop, is a special character to Apache; and the \ tells it to ignore the key and continue processing the rule, leaving the . as we see it.And there we have it. With that rule, when a user visits page1.html ; the webserver will load page2.html without them knowing they’ve been redirected!If you want your users to know they have been redirected; add [R] to the end of the line, and then they will be redirected fully, not transparently.
RewriteEngine On
RewriteRule ^page1\.html$ page2.html
More Advanced Application
Now we get to where mod_rewrite becomes really useful; when you start converting friendly urls into scripts. It’s not complex, and a single rule can define redirects for a whole directory; provided that they use the same pattern, (ie A-Z, 0-9 and a-z). For beginners, this can seem pretty complex, but once you get the hang of it, you’ll find that it’s well worth the effort Here is an example of this sort of application at work::This rule will match URL’s that start with go/ and are followed by any 2 digits, and a trailling / on the end. For instance, it will match go/12/ and go/98/ , and load the php page go.php?goid=12 or 98 , completely transparently.Now, explaining it. The square brackets are shown to contain the range, in this example it’s any 2 digits, and you can copy each section as many times as you like. You can also use [a-z] for lowercase letters; [A-Z] for uppercase letters and [A-Za-z] for any letters.The “regular” part of the string is encased in parenthesis, ( ), because we store whatever value is found here for later use. Once there is a value in a parenthesis, we can use it through what’s called a back reference. Each of the pieces of information you have placed in the parenthesis are assigned an index, starting with 1. So; the first back reference is $1, third is $3; so on and so forth. You can then recall them in the “rewrite to” section; like I have above, with $1.Once that’s done, the visitor can visit /go/12 and load the page go.php?goid=12 ; without knowing it, and without seeing the real url.The biggest problem with that is that if your visitor goes to /go/12 without the trailing / , the rule won’t recognise it, and they will just get a 404 error, so you then want to add a [R] rule which will add a / to the end, like so:
RewriteRule ^go/([0-9][0-9])/$ /go.php?goid=$1
RewriteRule ^go/([0-9][0-9])$ /go/$1/ [R]
before you add the previous rule, so this one is processed first. Then, when they visit /go/12 ; they will be redirected to /go/12/ and load the php page specified earlier.
Increasing the String Length
If you have a match term longer than 2 digits, for instance if your product is a word, like /products/awesomehat ; then you can use a symbol on the match to allow it to match an infinite length of that character, so /products/a/ would work, and so would /products/thisisareallylongproduct/It’s like telling Apache “one or more of the preceding character or range”::< br class="webkit-block-placeholder" />RewriteRule ^products/([a-z]+)$ /products/$1/ [R]
RewriteRule ^products/([a-z]+)$ /product.php?prodname=$1
And that’s it. Now, anything entered after /products/ that uses lowercase letters will be picked up by the rewrite engine, and will correspond to the PHP file, so visiting /products/thisisaproduct will load product.php?prodname=thisisaproduct ; exactly as it did before, but the length is now unlimited.I hope this helps you with Mod_Rewrite, and if you have any problems, please contact me You may not copy any part of this tutorial without express prior permission of myself.Thanks for reading