Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite: semi-clean URIs

need mod_rewrite advice...

         

damonweiss

5:47 am on Apr 8, 2007 (gmt 0)

10+ Year Member



Hi all,

I'm relatively new to mod_rewrite and am therefore seeking advice on how to create "semi-clean" URLs. By "semi-clean", I mean that would like some of the parameters in my query string to be rewritten within the URL, and some to remain as parameters appended as a query string. Generically, I am looking to rewrite:

[{baseURL}...]

as

[{baseURL}...]

Although I realize that this may sound odd, my reasons for wanting to do this are fairly sound. It is my understanding that one of the main purposes for rewriting URLS is to make them more readable and user-friendly. In my case, I am trying to create a series of web services that perform engineering design calculations. Engineering input parameters are often numeric and thus are LESS readable when the query string is reduced to a URL path. For example, let's say we are designing a bridge beam.

[{baseURL}...] may be considered a "clean" URL, but is ambiguous because one has no idea what 36, 20 and 50 represent. It is arguably more readable, therefore, to write:
[{baseURL}...]

Simultaneously, however, I may not want ALL of the parameters to stay in the query string. Other parameters may describe the beam more semantically, and would therefore be more readable as part of the URL. For example, I would like to rewrite:

[{baseURL}...]
as
[{baseURL}...]

Now, combining the two concepts, I would want to write:

[{baseURL}...]

as

[{baseURL}...]

Can anyone tell me how to do this? Here's what I have so far. I believe I am running into a problem specifically with the first question mark. Apache does not know how to deal with it and, although I have some idea why, I cannot figure out the workaround to achieve what I want.

RewriteRule ^beam/(steel¦concrete)?depth=([0-9]+\.[0-9]+¦[0-9]+¦\.[0-9]+)&width=([0-9]+\.[0-9]+¦[0-9]+¦\.[0-9]+)&length=([0-9]+\.[0-9]+¦[0-9]+¦\.[0-9]+)?$ beam.php?material=$1&depth=$2&width=$3&length=$4

Thanks in advance.

jdMorgan

1:47 pm on Apr 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not part of the URL, but rather, data attached to the URL to be passed to the resource (e.g. script) *at* that URL. Therefore, they are handled separately by mod_rewrite using the RewriteCond directive examining the %{QUERY_STRING} variable.

Therefore, you'd need something like:


RewriteCond %{QUERY_STRING} ^depth=([0-9.]+)&width=([0-9.]+)&length=([0-9.]+)$
RewriteRule ^beam/(steel¦concrete)$ /beam.php?material=$1&depth=%1&width=%2&length=%3 [L]

However, there may be no actual need to validate the depth/width/length parameters in the rule itself, and if not, then the same thing can be accomplished using just the rule:

RewriteRule ^beam/(steel¦concrete)$ /beam.php?material=$1 [QSA,L]

I've shortened/simplified the regular expressions in the first example to accept any mix of digits and decimal points. This speeds up the rule processing, and leaves it to the better-suited script to validate the parameters. It wouldn't be possible, for example, to output a meaningful error message from mod_rewrite if a parameter is missing or out of range, so this validation function is best left to your calculator script.

Jim