SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A mod rewrite question

    Hi all,

    I guess I'm not understanding some of the mod rewrite tutorials I've come across.

    I'm trying to add more than one variable to a rewrite rule, but I'm missing something.

    This works:
    Code:
    RewriteRule business/([^/]+)$ business/$1/ [L]
    RewriteRule business/([^/]+)/$ business.php?id=$1 [L]
    I'm trying to get something like this:
    Code:
    RewriteRule business/([^/]+)$/([^/]+)$ business/$1/$2/ [L]
    RewriteRule business/([^/]+)/$/([^/]+)/$ business.php?id=$1&name=$2 [L]
    However, I just get a 404 Not Found error. Could I get some assistance on this please?

    Thanks!
    No, I REALLY dislike having to use Joomla.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Not my strongest point but here goes.....!
    Code:
    RewriteRule business/([a-zA-Z0-9_]+)?/?([a-zA-Z0-9_]+)?/? business.php?id=$1&name=$2 [L]
    should rewrite business.php?id=1&name=spike
    to
    business/1/spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    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)
    spikeZ,

    Aw, mate, I think you've confused me!

    crow,

    What is the first RewriteRule supposed to do?
    Code:
    RewriteRule business/([^/]+)$ business/$1/ [L]
    RewriteRule business/([^/]+)/$ business.php?id=$1 [L]
    What that'll do is redirect business/{something} to business/{something}/ so you can redirect it (again) to business.php?id={something}. IMHO, the only thing that does for you is to guarantee that the relative links are relative to the business/{something} directory.

    Ditto the second group.

    Before I ask why these are two-step and the pairs are now combined, it's apparent that you're having a problem so ... what's the problem? If this is in the DocumentRoot's .htaccess file (and you also use RewriteEngine on), then it should work (because a 500 error would have indicated that mod_rewrite is not enabled). The 404 indicates that business.php is not in the DocumentRoot (or that any relative links are not associated with the {something} subdirectory, i.e., two levels lower in the physical file structure).

    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

  4. #4
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...and it does. Awesome!

    Thank you very much Spike!

    DK -

    To answer your question, I grabbed that snippet from a mod rewrite post on here a long time ago and have just re-used it. So, my knowledge of why/how is not very impressive.

    I wasn't having any problems if just passing one variable. So, that snippet was working fine. I was having trouble understanding why two variables (or more) were not working.

    business.php and the .htaccess file are both in the DocumentRoot and mod_rewrite is enabled.

    I'm pining over your explanation of the {something} directories...bear with me.

    Thanks again guys!
    No, I REALLY dislike having to use Joomla.

  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)
    Mike/crowden,

    The problem with the offered code is that the / was made optional so that it would also match whatever the single version would match - I believe the first atom's greed would allow the second atom's greed to only match the last character. My implementation of a one OR two atom (id and/or name) case (where both atoms are anything without /'s and without the trailing slash - it confuses browsers and requires you to use absolute links in your script) would be:
    Code:
    RewriteRule ^business/([a-zA-Z0-9_]+)(/([a-zA-Z0-9_]+))?$ business.php?id=$1&name=$3 [L]
    This requires the URI to begin with business/, captures all letters and digits and _'s then OPTIONALLY captures the same characters for name (inside an optional atom which also includes the / between id and name). IMHO, that is the way to make parts of a URI optional, not by making each part optional (you never know what you'd get if you do).

    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
  •