mod_rewrite rule

  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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]
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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]
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I'm curious about which CMS you're using, or whether you're using something in-house. :D
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

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 143 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.