Simple mod_rewrite Tutorial

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Simple mod_rewrite Tutorial

    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



    but will really be given



    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)


    into



    Instructions:
    Just make a .htaccess file and input the following:
    Code:
    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:


    You may be wondering what are the functions of those codes
    Code:
    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):



    to



    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:
    Code:
    <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>

    #2
    Thanks 4 it..
    she is beautifull than php.and i love her more than php.
    sigpic

    Comment


      #3
      nice tutorial. well explained

      CONS:
      -additional work
      -now everybody can boast hiding ses id lol
      LDSWAPWORLD sigpic
      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.


      Comment


        #4
        Originally posted by capofret View Post
        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..

        Comment


          #5
          yeh..Koizumi's right..this one doesn't show how to hide ses 'cause the value of sid should also be seen in the mod_rewritten url like /main?sid=$sid rewrite to /main-$sid..where rewrite rule is /main-(a-z0-9) /main?sid=$1 [NC]
          My Blog: http://jhommark.blogspot.com
          My Facebook: http://www.facebook.com/jhommark
          My Official Site: http://www.undergroundweb.tk
          My Community Site: http://undergroundwap.xtreemhost.com

          Comment


            #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...
            _______
            Info & Tips
            php.net
            w3schools.com

            Comment


              #7
              why mine..i've hidden the ses id..the script is lava
              My Blog: http://jhommark.blogspot.com
              My Facebook: http://www.facebook.com/jhommark
              My Official Site: http://www.undergroundweb.tk
              My Community Site: http://undergroundwap.xtreemhost.com

              Comment


                #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...
                _______
                Info & Tips
                php.net
                w3schools.com

                Comment


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

                  Comment


                    #10
                    Originally posted by icedroplet1987 View Post
                    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..

                    Last edited by koizumi; 12.10.09, 14:20.

                    Comment


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

                      Comment


                        #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...
                        _______
                        Info & Tips
                        php.net
                        w3schools.com

                        Comment


                          #13
                          maybe you could show us a preview or demo?

                          Comment


                            #14
                            Originally posted by 12345xmen View Post
                            can i also hide session id via this ???????????
                            you have to back read man..

                            Comment


                              #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...
                              _______
                              Info & Tips
                              php.net
                              w3schools.com

                              Comment

                              Working...
                              X