SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Manchester, UK
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Apache RewriteCond Question

    Hi,

    Will the RewriteCond directive with the [OR] flag go to the next RewriteCond in the list even if the next RewriteCond is not directly below first?

    Example (RewriteCond below are for example purposes only and not totally correct):

    RewriteCond %{HTTP_USER_AGENT} ^Mozilla.* [OR]
    ...
    ...
    ...
    ...
    RewriteCond %{HTTP_USER_AGENT} ^IE.* [OR]
    ...
    ...
    ...
    ...

    ... = rewrite rules satisfying condition(s)

    Dan

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    dan,

    Yes. Do not merely copy each RewriteCond statement (changing the regex) as the last one should NOT have the [OR] flag.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Manchester, UK
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi DK,

    thanks for taking the time to reply, the reason I ask this is because I am currently deciding which approach (out of 2) to take.

    The first being (where the id is hardcoded) :

    RewriteCond %{REQUEST_URI} ^referrals.* [OR]
    RewriteRule ^referrals/accountants/$ r.php?n=ReferralCategory&cid=1 [L]
    RewriteRule ^referrals/architects/$ r.php?n=ReferralCategory&cid=2 [L]
    .. at least another 10 rules as abvove
    RewriteCond %{REQUEST_URI} ^ask.* [OR]
    RewriteRule ^ask/accountants/$ r.php?n=AskCategory&cid=1 [L]
    RewriteRule ^ask/architects/$ r.php?n=AskCategory&cid=2 [L]
    .. at least another 10 rules as abvove

    The second being (id is taken from url):

    RewriteRule ^referrals/([a-zA-Z]+)/([0-9])$ r.php?n=ReferralCategory&cid=$2 [L]
    RewriteRule ^ask/([a-zA-Z]+)/([0-9])$ r.php?n=AskCategory&cid=$2 [L]

    The second option has the negative impace of having a number at the end of the url, as in www.example.com/referrals/accountants/22, but drastically cuts down on the number of rules. I think the second may be a lot more efficient, but at the cost of increasing the length of the url.

    Note: the rules presented are merely for example purposes and may contain mistakes.

    Thanks again
    Last edited by bustacapinya; Feb 13, 2008 at 08:40.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Manchester, UK
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The only probelm I can see with the second option above is that the url can easily be tampered with by users, for example :

    http://ww.example.com/accountants/22/

    Because its only the number (id) im interested in, a user could type anything in place of 'accountants' in the above example and still access the page, which in my opinion is not desirable.

    Dan

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    busta,

    Hmmm, looking at your "amplification," the answer is a definite NO.

    mod_rewrite (block) statements are terminated by the Last flag. You cannot extend a part of a block to another block as your code is attempting to do. It's a pain but you can set an environmental variable then look for that in successive blocks.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Manchester, UK
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK,

    Are you saying that :

    RewriteCond %{REQUEST_URI} ^referrals.* [OR]
    RewriteRule ^referrals/accountants/$ r.php?n=ReferralCategory&cid=1 [L]
    RewriteRule ^referrals/architects/$ r.php?n=ReferralCategory&cid=2 [L]
    .. at least another 10 rules as abvove
    RewriteCond %{REQUEST_URI} ^ask.* [OR]
    RewriteRule ^ask/accountants/$ r.php?n=AskCategory&cid=1 [L]
    RewriteRule ^ask/architects/$ r.php?n=AskCategory&cid=2 [L]
    .. at least another 10 rules as above

    is not possible? And could you possibly explain more what you mean, ofcourse the last rewrite rule will not contain [OR].

    Also I am decideing betwen the two options in my post above to use, Im not using them both

    Thanks

    PS Could I possibly send you my htacess file for you to have a look at? I dont really want to post it here.
    Last edited by bustacapinya; Feb 18, 2008 at 07:48.

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    busta,

    I'm not sure what you're trying to do with the RewriteCond lines.

    What I was saying was that, UNLESS terminated by a Last flag, each successive mod_rewrite statement is ANDed with the next one.

    Example:
    Code:
    RewriteRule ^/?([0-9]+)/test\.php$ $1/redirect1
    RewriteRule ^/?([a-z]+)/test\.php$ $1/redirect2 [L]
    In this example, using digits in the first subdirectory and using letters in the second subdirectory, redirect2 will NEVER be effected because the second RewriteRule is ANDed with the first and the digits can NEVER match the letters of the second.

    Using the Last flag is similar to using ; in a single line PHP statement OR using } to close a PHP block statement. PHP will only provide an error if those characters are not used while mod_rewrite will "dummy-up" and try to proceed with what it thinks you want it to do (AND all statements until it finds a Last flag).

    Yes (PM).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •