Forum Moderators: phranque

Message Too Old, No Replies

Impossible Query String for mod-rewrite?

I think I'm hosed, but need opinions

         

neophyte

1:03 am on Jul 14, 2009 (gmt 0)

10+ Year Member



Hello All -

Long ago I'd devised a dynamic navigation system that uses a rather unusual query string which looks like this: index.php?cmd=02.01.00.

When parsed by php this number equates to: "get the text for section two, page one, sub section none".

this six-digit number format (with dots) is the primary key designator for both navigation and text content tables in my database. My project directory structure is also based upon this idea.

This scheme works like a charm and I've been very happy with it until a client just said that he DOESN'T want query strings in the address bar, but user-friendly URLs.

Since this is an ADD-ON requirement for an already deployed project, 02.01.00 would have to be displayed in the address bar as: www.example.com/the_resort/accommodations

I didn't think this would be such a problem using mod_rewrite until I saw that, in order for mod_rewrite to work, it (apparently) needs query strings like section=02&page=01&subsection=00

Uh oh.

So, I'm feeling kind-of worried that - due to the way I've written the site and database - I won't be able to create user friendly urls for this project.

Am I right? Or is there some way mod_rewrite can parse a query like cmd=02.01.00 into it's various parts and then display it's user-friendly equivalent in the address bar? I've searched around the web on this but haven't found any kind of indication on how to handle this kind of challenge.

All advise/insight greatly appreciated.

Neophyte

jdMorgan

2:07 am on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, I'm feeling kind-of worried that - due to the way I've written the site and database - I won't be able to create user friendly urls for this project.

Am I right?

No, mod_rewrite can happily parse any query string that can be described using regular expressions. But...

Or is there some way mod_rewrite can parse a query like cmd=02.01.00 into it's various parts and then display it's user-friendly equivalent in the address bar?

unfortunately, mod_rewrite isn't intended to "change the address bar" or change URLs. Mod_rewrite works on the server, not in the browser. If URls are to be changed, they need to be changed on your HTML pages, because those pages define the URLs. So you are thinking entirely backwards about this problem.

OK, so that's all the bad news. How about an alternate plan?

Let's change all of the links on this client's pages by having mod_rewrite pass any requested URL-path of the form "/the_resort/accommodations" -- that is, one "directory level, plus an extensionless file" to
your script as "/index.php?where=the_resort&what=accomodations". Your script can then look up the "key number" value of "02.01.00" using the "what" and "where" values as database lookup keys.

Then, later in the script code as you are generating the HTML page to send back to the client, replace all link URLs that would have been in the form "/index.php?02.01.00" with your client's preferred format of "/the_resort/accomodations". preg_replace in PHP is great for this, once you've retrieved the replacement URL-text from the database.

This is the "customary" solution to this problem. The only detail left is to recognize those URLs when a visitor clicks on a "/the_resort/accomodations" link on the HTML page and thereby requests that URL from your server. One line of mod_rewrite can code do that, and pass the request on to your script:


RewriteRule ^([a-z_\-]+)/([a-z]+)$ index.php?where=$1&what=$2 [L]

Jim

neophyte

10:28 am on Jul 14, 2009 (gmt 0)

10+ Year Member



Jim -

Thank you so VERY much for your detailed response. After a bit of though, I now see what you're talking about and how I can do a script-based work around in order to retain the current state of this project's database.

I'd like to ask one as on if I may - you mentioned that what you are suggesting is the "customary solution to this problem". Is that meant to say that others do their query strings in a similar fashion to mine which also require "scripting intervention" and a bit of mod-rewrite in order to product friendly URLS as you are suggesting to me?

Neophyte

jdMorgan

5:28 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I only meant that since only a script can query your database to establish the 'association' between the 'friendly' URL-text desired by your client (your customer, that is, not an HTTP client), that the solution requires the multi-step process described above. On its own, mod_rewrite has no way to know that the two 'URLs' are associated or how to map one to the other, since it cannot open and query your database without scripted assistance.

So, the two common solutions are to modify the existing script and rewrite all appropriate requests to that script, or to write a second (small) script using PERL and invoke it in the URL-to-filename translation phase of the API using a RewriteMap (if you have the server privileges required to define a RewritMap, that is).

In your case, it would be simpler to modify your own script. When using off-the-shelf scripts on VPS or dedicated servers where you have config privileges, it's often easier to use the RewriteMap solution, since you don't want to have to re-modify an off-the-shelf script every time the vendor updates it.

Jim

neophyte

1:18 am on Jul 15, 2009 (gmt 0)

10+ Year Member



Okay Jim, got it. Thanks again for your assistance.