[Translation] Mod_Rewrite tutorial

Discussion in 'Forum for discussion of ANTICHAT' started by Дрэгги, 13 Mar 2007.

  1. Дрэгги

    Дрэгги Elder - Старейшина

    Joined:
    26 Aug 2005
    Messages:
    284
    Likes Received:
    396
    Reputations:
    182
    mod_rewrite...what is it? As wiki says -

    i.e. some kind of module for a web server, enabling to modify links...In general, the rewrite engine itself exists both for apache and microsoft's IIS and even for java platforms =) In this particular article we're going to talk exactly about mod_rewrite for apache.

    Everything we're gonna need is .htaccess file radically, if you already have it..then it's only need to finish writing some certain lines in it..and surely in case hoster hasn't installed this module yet, he should be kicked around a little..)

    As for me personally I use full links in .htaccess, if you want to use relative links like /index.php?blabla=31337 then it's advisable to include the following lines in the file, which ,even though only a little bit, but speeds up the work

    that is, in case you have a www site but the option isn't on then apache will be trying to find Index.php file first in /home/ then in /home/htdocs/ and in the end will stumble on /home/htdocs/www/index.php. This is nothing of course, but you'll still spare some hundredth parts of the second..) or you can write in every file before "rewrital on" the following phrase

    PHP CODE:


    Generally it is possible to find many ways to use in it..most popular of which - is the transformation of the link into some more readable look ) ..well, find me a man who would like the link such as

    Let us transform it into..for instance


    For this we will need to turn RewriteEngine itself on for starters:

    PHP CODE:

    then we overwrite the url itself:


    PHP CODE:

    Let's clarify things related to this quite simple construction...

    RewriteRule simply sets the beginning of the overwrite line.

    ([a-z]+) indicates that everything before "slash" is replaced with small roman letters from a to z (everything is written into the $1 variable)

    then after "slash" we see ([a-z0-9]+).htm
    , that is, here we replace everything after "slash" with small roman letters from a to z and digits from 0 to 9 ($2 variable)
    , and then add htm extension in the end of the url.

    As you understand, such .htm files never actually were and will never be on your site, it's just that thus links are shown in a more comfortable way.

    The most important here is to remember this order - how it goes after RewriteRule - firstly - how to transform and only then what to transform


    As you understand that's in order to make forum fully overwrite links (urls) with parameters in htm look (not obligatory htm, it can be any kind of extension your imagination might create) for all existing transferrable by GET parameters...should it be a search, threads, forums, personal messages after all...the main thing is also not to overdo it = )

    Original copyright: blackybr

    English version translation: Дрэгги
     
    #1 Дрэгги, 13 Mar 2007
    Last edited: 5 Apr 2007
    17 people like this.
  2. NeMiNeM

    NeMiNeM Elder - Старейшина

    Joined:
    22 Aug 2005
    Messages:
    480
    Likes Received:
    308
    Reputations:
    201
    Good. The only big mistake I've noticed is when we speak about file types(I mean .htm .exe .doc etc) we say extensions not expansions. (expansion is mainly a physical term).
    And I've checked the original article, there is not so many information left, so please don't create a new thread "part two" but add that here. Thank you.

    +5
     
    1 person likes this.
  3. Дрэгги

    Дрэгги Elder - Старейшина

    Joined:
    26 Aug 2005
    Messages:
    284
    Likes Received:
    396
    Reputations:
    182
    Let's have a look at another example (here I already use relative links), let's make out of this

    rewriting it into

    Firstly, as I have mentioned above, enable

    PHP code:

    then


    PHP code:

    First line is understandable let's have a closer look into the second one. ([^/]+) - before "slash"
    we allow the use of any symbols (imagine we will have a digit in our forum title...though something like ([a-zA-Z0-9]+) is possible, but here however the dash won't be displayed).

    After ([0-9])-(.+) that is, in the second part of the "final file" we insert the variable $2 itself (which is an equal to "hacker"), and in the first one we insert only digits (in our case it is, for instance, the user ID which is an equal to 31337 ( ([0-9]) ). And certainly we add an extension in the end. The second part of the line needs no comments. . i.e there it's only indicated what is to rewrite. All three variables' values.

    I'll go over explaining the regular variables themselves once more:




    In addition to this the RewriteCond may be used, that is not the rule of the rewrite is set, but the CONDITION (!!!).
    They have similar syntax.


    what we can do to them... let's look into it using this example:


    PHP code:

    Moreover, this arises


    PHP code:


    where #forum is forum's title, #topicID is the ID of the topic.


    All the variables are given number in accordance with their appearance and the condition and the rule both have 2 variables (in this particular example naturally they have, but generally speaking even one hundred if you like, no one prevents you from doing this), which are being set by round brackets, because in order to use them you'll need to put them in there where they are exactly needed by you in the final link-result.


    Let's combine both the rule and the condition:


    By this we'll rewrite the link with the numerous parameters into the following type

    PHP code:



    In general it'll do even without "slashes", but IMHO the inscriptions will merge = )


    Here I've had in mind quite funny regular thing like this:


    PHP code:



    Don't you find it a little weird upon reading? = ) You're right...it rewrites everything based on a rule everything except any kind of symbol, I myself haven't tried it yet, but whoever checks what apache will spit out upon this one please ..requesting to write it here = )

    So.. We've learnt how to use RewriteRule and RewriteCond, using mod rewrite in order to provide the safety of the server (for example from sql inj) and simply using it in order to decorate and memorize the links.

    Thank you for reading :)

    2nd part coming up

    Original copyright: blackybr
     
    #3 Дрэгги, 18 Mar 2007
    Last edited: 19 Mar 2007
    3 people like this.
  4. Talisman

    Talisman Elder - Старейшина

    Joined:
    22 Apr 2006
    Messages:
    401
    Likes Received:
    153
    Reputations:
    80
    PHP code:
    Цитата:
    RewriteRule ^([^.]+)$ /index.php

    WHY this cod is php??? :D its cod from .htaccess file! apache-file!
     
    3 people like this.
  5. Дрэгги

    Дрэгги Elder - Старейшина

    Joined:
    26 Aug 2005
    Messages:
    284
    Likes Received:
    396
    Reputations:
    182
    Mod Rewrite Tutorial Part3

    A few words about how it is possible to use mod rewrite in order to increase the security of a site, as well as for content, access rights limitations etc. and so on (without using web programming languages).
    What else is mod rewrite capable of? Well it can do quite a lot of things. As I mentioned above, RewriteCond
    determines the rules of a condition and always
    preceds the RewriteRule , i.e the RewriteRule in this case works only under a true condition. Rewrite module accepts server variables as well which is undoubtedly pleasing. )

    Here are some of them which have to be used quite often:

    HTTP_USER_AGENT - the user agent (for instance Opera/9.10 (Windows NT 5.1; U; ru))

    HTTP_REFERER - referer (i.e., a site which you came from to an original, final site)

    HTTP_COOKIE - cookies being transferred =)

    HTTP_FORWARDED - the address of the user who is currently on the page

    HTTP_HOST - the address of a site (without http: //)


    HTTP_PROXY_CONNECTION - set in the event when the client has come via "transparent proxy-server

    HTTP_ACCEPT - a more precise definition of the informatio type (the so-called media-types) accepted by a browser on the given page, for example text/plain, text/html, image/gif, image/jpeg)


    REMOTE_ADDR - your ip-address

    REQUEST_METHOD - a method used for the delivery of the inquiry (GET, PUT, POST and so on)


    SCRIPT_FILENAME - a full way to an executable script on the server


    PATH_INFO - everything following the name of a script being initiated


    QUERY_STRING - the line of an inquiry




    Let's assume that we have certain site: http://site.com
    For some reason we totally don't want certain someone to have access to some file type, for instance, pictures jpg, png (well meaning that only scripts could do it). It is possible to try to do it by means of mod_access, something having .htaccess of sorts in its content root:

    PHP CODE:

    Well, the defect here is that it will forbid the access for both you and scripts triggering/opening pictures. We do it with the help of mod_rewrite:


    PHP CODE:



    First two lines here are clear. Further along we see a condition - when HTTP_REFERER contains site.com (regular variables see above) then the viewing of a picture is allowed.

    When can it be helpful? Well let's suppose we have a photoarchive and there is a counter and we wish to know the exact number of visitors. But actually when the photo is indexed in a search engine, and the person follows the link from that same yandex - in "referer" the yandex itself is indicated and it's not for the fact that the link will be directly on a picture. The so-called hot-linking. I think it is understandable:)



    Now let's make an authorization of sorts. Using Mod_rewrite again of course..
    Since I have already told that we shall not use php, perl etc. we will have to manage without the database as well =)


    For this purpose let us investigate one more directive of Mod - the RewriteMap which is used in the rules concerning the replacement by different mapping functions relative to different correspondence areas with the help of the rewrite rule via rewritecond..


    The authorization we will process through the sessions file in auth.txt which is situated outside of WWW. Let us assume that the full way on the server to the site is /home/user/www/

    Let's create a file /home/user/auth.txt having this kind of content:


    PHP CODE:



    Moving further along let's see what we will have inside the .htaccess


    PHP CODE:



    or for a file looking like (let's use a more secured version - session + IP)


    PHP CODE:





    we'll get a slightly different result


    PHP CODE:





    Everything which is done here I have already described. I'll repeat one more time -


    We take the QUERY_STRING (i.e. the line of an inquiry) and verify whether it has the session through the .txt file (the same can be done through the .php file addressing the database which will yield the same results).



    RewriteCond%2-%{REMOTE_ADDR} ^(.+)$ - %2 is taken from the first RewriteCond, our session at first, then the variable of the session and the IP-address (we have it in the .txt file too) - all of it we include in one variable - %1




    RewriteCond ${sessionids:%1|0} ^0$




    ${sessionids:%1|0} - the verification on RewriteMap

    sessionid - the name of the Rewrite Map itself



    %1 - the variable which I have already mentioned above


    0 - just the default variable


    The gist of it is that in case the verification on the file returns the positive result and the line with the data exists for the Rewite Map in question, then 1 is returned (that exact "one" I put in the end of every line in the file) auth.txt, in the opposite case 0 is returned ( ^0$ means something like "let's associate 0 with it"). If the verification didn't take place at all, then still the same - 0 is returned, after which mod rewrite goes on with its work, having sent Forbidden ( [F] ) first.


    (c) blackybr


    Also see http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
    http://en.wikipedia.org/wiki/Rewrite_engine




    English version translation: Дрэгги
     
    #5 Дрэгги, 4 Apr 2007
    Last edited: 5 Apr 2007
    4 people like this.
  6. KPOT_f!nd

    KPOT_f!nd положенец общага

    Joined:
    25 Aug 2006
    Messages:
    1,074
    Likes Received:
    502
    Reputations:
    65
    Дрегги - very good, as I have understood this translation of the article blackybr
    throw up the russian original article? please!
     
  7. Дрэгги

    Дрэгги Elder - Старейшина

    Joined:
    26 Aug 2005
    Messages:
    284
    Likes Received:
    396
    Reputations:
    182
    Russian original article you say? Sheesh, you could've easily found it by using search. Anyway, here's the original: http://forum.antichat.ru/thread27498-mod+rewrite.html
     
  8. NeMiNeM

    NeMiNeM Elder - Старейшина

    Joined:
    22 Aug 2005
    Messages:
    480
    Likes Received:
    308
    Reputations:
    201

    In this case, next time please write not only the nickname of the author but also give the link to the original article.
    And why don't you correct the mistakes I tell you? (e.g. "expansion" in the first post). And could you mark the main terms and keywords with special colours (like you did in the first part)? It would make your articles easier to read.

    Anyway, thank you for the translation. Good job.
    +7
     
  9. Дрэгги

    Дрэгги Elder - Старейшина

    Joined:
    26 Aug 2005
    Messages:
    284
    Likes Received:
    396
    Reputations:
    182
    Alright alright already Mr.English teacher. *rolls eyes* I forgot or was too lazy.:)

    Ummm..I'm not sure...I thought I'd leave that up to you. I basically left everything the same way as it was in the original article except the colours inside php-code thingies. Do you know how to make the php-code quotes look the same as they were in that article? Also me not using colours means that the author didn't use them either.
     
    #9 Дрэгги, 5 Apr 2007
    Last edited: 5 Apr 2007
    2 people like this.
  10. NeMiNeM

    NeMiNeM Elder - Старейшина

    Joined:
    22 Aug 2005
    Messages:
    480
    Likes Received:
    308
    Reputations:
    201
    I'm not going to teach you English=) You've already got your diploma and you are on a higher level than I am. I haven't got any degree yet so it's you who can teach me, correct or give tips;) It's just quite strange for me that a person is lazy to correct his own mistakes... But let's stop this "flame" and offtop.
    Easy.
    [*PHP][*/PHP]. This tag can be found on the bar together with quote,code,html etc tags.

    Sure. It's up to you. It was just an advice.
     
    1 person likes this.
  11. KPOT_f!nd

    KPOT_f!nd положенец общага

    Joined:
    25 Aug 2006
    Messages:
    1,074
    Likes Received:
    502
    Reputations:
    65
    Дрегги thank you =) you has rescued me =)
     
  12. banned

    banned Banned

    Joined:
    20 Nov 2006
    Messages:
    3,325
    Likes Received:
    1,194
    Reputations:
    252
    Code:
    translate.google.com
    Sorry KPOT_f!nd, but you are speaking English very poorly .. And so mod_rewrite rules =)
     
  13. Дрэгги

    Дрэгги Elder - Старейшина

    Joined:
    26 Aug 2005
    Messages:
    284
    Likes Received:
    396
    Reputations:
    182


    The only mistake he made was "have" instead of "has".

    However posts like this will be deleted in the future due to their insignificance.
     
    1 person likes this.
  14. darky

    darky ♠ ♦ ♣ ♥

    Joined:
    18 May 2006
    Messages:
    1,773
    Likes Received:
    825
    Reputations:
    1,418
    Really surprised to see it translated =) Next comin' the 3rd part..

    PS hm... u should write you have or in our situation you had rescued me =))) English speakin' mazafaka )))
     
    1 person likes this.
Loading...