Simple mod_rewrite Tutorial

Started on the old forum in October 2009 · view the original in the archive · old links redirect here

29 replies · 3,322 views · started 17 years ago · 3 min read

11 participants
koizumi Developer · 81 posts #1

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>
ranzit2 Developer · 187 posts #2

Thanks 4 it..

she is beautifull than php.and i love her more than php.

capofret Developer · 225 posts #3

nice tutorial. well explained

CONS: -additional work -now everybody can boast hiding ses id lol

LDSWAPWORLD site closed. im busy with other things in life like facebook , send me PM so i can add you 🙂 www.pinoySG.com

don't ask for help if you're not even helping yourself! i am tired of seeing the line "best site", i want to see something NEW and UNIQUE. maybe if i find one, ill go back to my wap life again.

External image (may be gone)

koizumi Developer · 81 posts #4

capofret wrote: nice tutorial. well explained

CONS: -additional work -now everybody can boast hiding ses id lol

lol.. haha. yeah! tottaly lots of additional work..

but this one actually doesnt hide the the ses/sid or whatever.. it will still be part of the url.. the only thing you can hide ses/sid is by cookies or something like that..

subzero Super moderator Fellow · 3,954 posts #6

lol well it will not hide your sid becuz its lavalair

also note building a new script with better ways is the idea.

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

subzero Super moderator Fellow · 3,954 posts #8

but i still can steal it lmao

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

icedroplet1987 Senior · 258 posts #9

lol nice copy paste.. sud i give u the web url from where it got copy?

koizumi Developer · 81 posts #10

icedroplet1987 wrote: lol nice copy paste.. sud i give u the web url from where it got copy?

here is it, mod_rewrite, a beginner's guide (with examples) now everyone can decide if its a copy paste or not.. notice my examples, codes, pros and cons, explanation of every code in the mod_rewrite. i dont think i can copy my own words from anywhere.. lol..

thanks for everyone who thanked me for my efforts and those who are insecured..

😊

12345xmen Developer · 239 posts #11

can i also hide session id via this ???????????

subzero Super moderator Fellow · 3,954 posts #12

well mine new script will have 5 users are lucky to get for free

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

koizumi Developer · 81 posts #13

maybe you could show us a preview or demo?

subzero Super moderator Fellow · 3,954 posts #15

nah its still in the making 😉 some users here seen it

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

koizumi Developer · 81 posts #16

maybe i can take a peek? lol..

riderz Lead · 975 posts #17

yea subzero new site are totally awsum love it cant wait till its finish but how do they say for all the wait there are good to come


Jacques jacques@gw-designs.co.za http://coding.biz.tm Come join and lets make it a place to learn all the noobies how to code


NEVER FORGET TO CLICK THE TANX BUTTON IF U LIKE WHAT IM SHARING OR HELPING WITH

metulj Super moderator Principal · 1,726 posts #18

subzero wrote: well mine new script will have 5 users are lucky to get for free

am i lucky enough? 😳

It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt. ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ ! ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.

キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

subzero Super moderator Fellow · 3,954 posts #20

metulj wrote: am i lucky enough? 😳

Yes your are lucky for it 😉

kiLLeR-eyEd_14 wrote: how.,why don't u show me?

Postin undone script will class me of spamming it lol

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close