SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question RewriteRule ${map:key} flag

    My O'Reilly Apache Pocket Guide shows a flag for the RewriteRule that's listed like this:

    Code:
    ${map:key [|def]}
    Can anyone give me an example of it being used and tell me a bit more about how it works/what it does?

    What I'm trying to do is this... I want to take a URL like this:
    Code:
    index.cfm/go/2/cat/B
    and rewrite it to this:
    Code:
    /photogalleries/BestOf
    (photogalleries is a php page, and BestOf is the parameter being sent to it -- this is all set up and works).

    There are many such categories, each represented by one letter in my old site. I can use the following rewrite rule...
    Code:
    RewriteRule ^index.cfm/go/2/cat/(B | N | S | G | H | W)/?$ /photogalleries/xxxxx
    ... to capture those url requests, but I'd like to be able to automatically map the value of xxxxx based on what $1 is.

    So I am wondering if this map:key flag will do the trick. I can do it with multiple rules, each handling one parameter (and I will if it comes to that), but I would like to do it with one rule using the power of mod_rewrite and regexp if possible.

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi :-),

    >> ${map:key [|def]}
    >> Can anyone give me an example of it being used
    >> and tell me a bit more about how it works/what it does?
    Yes a real example could help to better understand the RewriteMap directive.

    Example, really simple:

    You want to redirect some users to a particular page.
    You also want to store the relationship user--page into a separated file and you also don't want to edit the rewrite rules each time you add a new user.

    That issue can be solved using the RewriteMap directive, so we could use:

    httpd.conf
    Code:
    RewriteEngine On
    
    RewriteMap user2page txt:c:\usr\user2page.txt
    
    RewriteRule ^/user/(.+)$ ${user2page:$1|/nouser.php}
    c:\usr\user2page.txt
    Code:
    pippo /path/to/pippo.php
    pluto /other/path/to/pluto.php
    doing that when we will have to add an user we need to edit only a text file ( or a php script can generate it ).
    Remember that the file c:\usr\user2page.txt is cached internally so usually it's read only at startup or when the file is changed.
    RewriteMap can be used only inside httpd.conf though .

    Let me explain it better:
    >> RewriteMap user2page txt:c:\usr\user2page.txt
    here I'm saying:
    create a map ( a ``function'' ) named user2page,
    this ``function'' will search into a file called c:\usr\usr2page.txt

    >> RewriteRule ^/user/(.+)$ ${user2page:$1|/nouser.php}
    here I'm saying:
    redirect all ``users'' to a page.
    The name of the page is obtained by the ``user2page'' function passing to it as argument the user name.
    If the user2page doesn't find a user with that key the value /nouser.php is used as default.

    Speaking in php is something like:

    function user2page( $username )
    {
    // search if inside c:\usr\user2page.txt
    // there is a key called $username
    // if so it returns the values of that key
    // else it returns /nouser.php
    }

    So basically,

    REQUEST_URI: /user/pippo
    it will be redirected internally to
    /path/to/pippo.php

    REQUEST_URI: /user/pluto
    it will be redirected internally to
    /other/path/to/pluto.php

    REQUEST_URI: /user/unknown
    it will be redirected internally to
    /nouser.php

    Hope It was a bit more clear now,
    eventually do not hesitate to ask :-)


    Thanks,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  3. #3
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Back to your issue, my previously was an explanation on a way to use RewriteMap.

    You can use:


    httpd.conf
    RewriteEngine On
    RewriteMap catname txt:/path/to/catname.txt
    RewriteRule ^/index\.cfm/go/2/cat/([A-Z]+)/?$ /photogalleries/${catname:$1}


    /path/to/catname.txt
    B BestOf
    N NetBSD
    S suexec
    G grep
    H help
    W wget



    If you don't have access to httpd.conf then you can't use RewriteMap,
    ...so let me know.



    Thanks,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  4. #4
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pippo
    [snip]
    If you don't have access to httpd.conf then you can't use RewriteMap,
    ...so let me know.

    Thanks,
    :-) Andrea
    Oh I don't have access to httpd.conf (not on my host anyway). But thanks for your great info, I understand now, and that would certainly be the easy way to do it if I did have access to httpd.conf.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •