So, what is mod_rewrite for?
Simply, mod_rewrite is used for rewriting a URL at the server level, giving the user output for that final page. So, for example, a user may ask for
http://www.somesite.com/widgets/blue/
but will really be given
http://www.somesite.com/widgets.php?colour=blue
by the server. Of course, the user will be none the wiser to this little bit of chicanery.
Pros: -User friendly URL, its more easier to memorize -More secure, as it will hide the original location and file name of a certain file -and most of all, its SEO friendly
Cons: -I haven't met something so far, it anyone would like to share, i will appreciate it.
Here is it. for this tutorial, the example i'm using is a lavalair script.
In this tutorial, i will show you how to transform:
(the login page of a usual lava script where username is lorem and password is ipzum) http://localhost/login.php?loguid=lorem&logpwd=ipzum
into
http://localhost/login/lorem-ipzum
Instructions: Just make a .htaccess file and input the following:
RewriteEngine on
RewriteRule login/(.*)-(.*)/?$ login.php?loguid=$1&logpwd=$2 [L]
save it on the site directory and your done.
You can now login to your site via this url: http://localhost/login/lorem-ipzum
You may be wondering what are the functions of those codes
RewriteEngine on
RewriteRule[COLOR="Blue"] login[/COLOR][COLOR="Green"]/[/COLOR][COLOR="Red"](.*)[/COLOR][COLOR="Green"]-[/COLOR][COLOR="Red"](.*)[/COLOR]/?$[COLOR="Magenta"] login.php?loguid=[/COLOR][COLOR="Violet"]$1[/COLOR][COLOR="Magenta"]&logpwd=[/COLOR][COLOR="Violet"]$2[/COLOR] [L]
RewriteEngine on = opening instruction
RewriteRule = specify the rule
Blue = the desired name for your URL to be rewriten
Green = as you noticed, i used slash and a dash, well any character can be used, its up to your preference. Those will serve as the spacer.
Red = the enclosing brackets signify that anything that is matched will be remembered by the RewriteRule, as you may notice there are 2. In the example i will only need 2, 1 for loguid and 1 for the logpwd.
/?$ = Makes sure that the only thing that is found after what was just matched is a possible forward slash, and nothing else. If anything else is found, then this RewriteRule will be ignored.
Pink = the original filename/url you want to be rewritten
Light Pink = $1 $2 they correspond with the value in the Bracket or the one colored in Red. The first bracket is $1, the second bracket is $2, if you will need another value, you can use another bracket and the value of it will be $3, and so on.
[L] = Tells Apache to not process any more RewriteRules if this one was successful.
Another example(Rewriting the URL's in a Lava Script):
http://localhost/index.php?action=main&sid=12345
to
http://localhost/home/main/12345
its just like the first example, all you have to do is add this line to your .htaccess,(dont forget to move the [L] tag to the end of the line of your rewrite codes)
RewriteRule home/(.)/(.)/?$ index.php?action=$1&sid=$2
and in your login.php, you should have this:
<p>
Welcome back to!!<br/>
Thats all for now.. lol..
You may bookmark this page for later autologin.
<br/><br/>
Click to enter:<br/>
<a href=\"home/main/$sid\">Continue</a>
<p>