Flexible URLs -/foo/bar/baz/qux/ to ?foo=bar&baz=qux

Discuss practical ways rearrange URLs using mod_rewrite.

Flexible URLs -/foo/bar/baz/qux/ to ?foo=bar&baz=qux

Postby shaynesweeney » Thu Aug 07, 2008 1:50 pm

Hello all! First time caller, long time listener...

So I have been pulled in for questioning, regarding a rewrite task that has been passed down to a developer at work.

We're moving from an IIS setup, using Helicon 3 (for rewrite) to Apache :)

The structure mentioned below is unfortunate, however - we're not able to change it, yet... Also assume that we can't process this by sending the URI to another page. (e.g. Like sending "/foo/bar" to a PHP script to disect). With that said, I agree this isn't a great approach nor a smart one.


How can I take a URL like "/my.file/foo/bar/?baz=qux" and convert it to "/my.file?foo=bar&baz=qux".

Sounds easy? Well, there's a kicker, at least it was for me ... How do you make it support an "endless" list of parameters... going beyond /foo/bar/ to /foo/bar/baz/qux/quux/quuux/ etc...


Any insight would be greatly appreciated!


Shayne
shaynesweeney
 
Posts: 3
Joined: Thu Aug 07, 2008 1:43 pm
Location: California, USA

Eureka!

Postby shaynesweeney » Thu Aug 07, 2008 2:10 pm

Figured it out! (partially)

RewriteCond %{REQUEST_URI} ^/index\.cfm/.+
RewriteRule ^my.file/(([^/]+)/?([^/]+)?)/?(.*)? /my.file/$4?$2=$3 [QSA]



But Doh! Only works in an .htaccess? What needs to change to make this work in a VirtualHost directive?
Last edited by shaynesweeney on Thu Aug 07, 2008 3:08 pm, edited 1 time in total.
shaynesweeney
 
Posts: 3
Joined: Thu Aug 07, 2008 1:43 pm
Location: California, USA

Postby shaynesweeney » Thu Aug 07, 2008 3:07 pm

Some more messing around... Here's my final version:

RewriteCond %{REQUEST_URI} ^/my\.file/.+ [NC]
RewriteRule ^/my.file/(([^/]+)/?([^/]+)?)/?(.*)? /my.file/$4?$2=$3 [NC,QSA,N,C]
RewriteRule . - [L]


Not sure if that last rewrite rule is getting processed ... the idea there was to keep it from processing more rewrite rules
shaynesweeney
 
Posts: 3
Joined: Thu Aug 07, 2008 1:43 pm
Location: California, USA


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 46 guests

cron