mod_rewrite rule

  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post May 16th, 2010, 7:35 pm

I'm trying to make a rewrite rule to map
Code: [ Select ]
/alpha/beta/gamma/delta.html?var1=a&var2=b

to
Code: [ Select ]
/?id=alpha:beta:gamma:delta&var1=a&var2=b


I'd like it to be able to handle a variable number of directories (alpha, beta, gamma, etc) and a variable number of supplemental var pairs as well.

Any help would be appreciated.

Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 16th, 2010, 7:35 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 16th, 2010, 9:58 pm

Is your use of colons a requirement set in stone ?

If you can work with having directory separators instead of colons, something like this might work for you.

APACHE Code: [ Select ]
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html ?id=$1 [QSA,L,NC]
  1. RewriteBase /test/
  2. RewriteCond %{REQUEST_FILENAME} !-f
  3. RewriteRule ^(.+)\.html ?id=$1 [QSA,L,NC]
Strong with this one, the sudo is.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post May 17th, 2010, 6:45 am

Thanks joebert, I completely spaced on the QSA flag, you have no idea how much I have been beating my head against the wall about this.

As for the colon, it comes from the internal way that the CMS formats its ids. I will go through the source and see how much of a pain it will be to modify that to use slashes instead of the colons, but if there is any way to basically do a find/replace on the rewrite var it would save me a whole lot of hassle.

Thanks.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 17th, 2010, 3:22 pm

Well, depending on your configuration access, you may be able to use RewriteMap with an External Rewriting Program to do it. RewriteMap is only available in the server config or virtual host context though, not htaccess. Well, technically the map created is available in htaccess via RewriteRule, but the RewriteMap directive which defines the map isn't.

That will give you a pretty much unlimited variability.

If you know there will never be more than say, X directories, you can use a collection of RewriteRule statements that starts with one that would match the max number of directories down to just the filename.

Something like

APACHE Code: [ Select ]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+)\.html$ /?id=$1:$2:$3 [QSA,L
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)/([a-z]+)\.html$ /?id=$1:$2 [QSA,L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)\.html$ /?id=$1 [QSA,L]
  1. RewriteCond %{REQUEST_FILENAME} !-f
  2. RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+)\.html$ /?id=$1:$2:$3 [QSA,L
  3.  
  4. RewriteCond %{REQUEST_FILENAME} !-f
  5. RewriteRule ^([a-z]+)/([a-z]+)\.html$ /?id=$1:$2 [QSA,L]
  6.  
  7. RewriteCond %{REQUEST_FILENAME} !-f
  8. RewriteRule ^([a-z]+)\.html$ /?id=$1 [QSA,L]
Strong with this one, the sudo is.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post May 20th, 2010, 7:59 am

Thanks Joe, I ended up just writing a bunch of static rules like you suggested, and then having it bounce the user to an error page if it goes over the specified number of rules.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post May 20th, 2010, 3:40 pm

I'm curious about which CMS you're using, or whether you're using something in-house. :D
Strong with this one, the sudo is.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 25, 2004
  • Posts: 734
  • Status: Offline

Post May 24th, 2010, 6:19 am

It's this old, kludgy in-house thing that was developed years ago and the management won't let us get rid of, it's kinda like their baby blanket actually... So we get stuck making sketchy patches like this to implement new functionality because it's easier than trying to decipher the poorly written ancient code base.

Fun times :-)

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 153 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.