A mod rewrite question

Hi all,

I guess I’m not understanding some of the mod rewrite tutorials I’ve come across. :confused:

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

This works:

RewriteRule business/([^/]+)$ business/$1/ [L]
RewriteRule business/([^/]+)/$ business.php?id=$1 [L]

I’m trying to get something like this:

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!

…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. :slight_smile:

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!

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:

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

spikeZ,

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

crow,

What is the first RewriteRule supposed to do?

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

Not my strongest point but here goes…!


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