views:

320

answers:

1

Here's what I have now:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(.*)$ /$3?$1=$2 [N,QSA]

This turns www.any.com/x/20/y/30 into www.any.com/index.php?x=20&y=30
(while ignoring existing directories - thank you so much Gumbo!!)

RewriteRule ^([^/]+)/([^/]+)(.*)$ /$3?$1&$2 [N,QSA]

This tweak allows me to go: www.any.com/x/y=30 and accomplish the same results but with x having no value. I love having this option, but I have to do two variables at a time or I get a page not found for the odd variable. So www.any.com/x/y=20/z would give me "url /z not found!"

Any thoughts on how I can get it to rewrite basically every folder into a request variable? Thanks, guys.

A: 

Try these rules:

RewriteCond %{REQUEST_FILENAME} -f  [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([^/=]+)=([^/]*)(/(.+))?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)/([^/=]+)(/(.+))?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)(/(.+))?$ /$3?$1 [N,QSA]

The first is to abort the rewriting process if request can be mapped on an existing file or directory. The second rule is for URL paths of the form /foo=bar/…/…?foo=bar. The third is for URL paths of the form /foo/bar/…/…?foo=bar. And the fourth is for URL paths of the form /foo/…/…?foo.

Gumbo
Wow, you really get this stuff. Perfect, Gumbo. Thank you once again.
Aaron
Hey Gumbo, just wanna say again, this has been solid gold for my project and even my education. There's only one tiny bug I can see, it's hardly worth mentioning, but if you have any idea why in some browsers this might cause a script to execute twice, I would be all ears. So far it's just Safari (maybe only on macs) and only on the second, third, fourth, etc reload. If not I can definitely live with it!
Aaron
@Aaron: I can’t image how mod_rewrite could cause that issue. Because all requests are treated as equal. So I think it’s rather Safari that’s requesting the resource twice.
Gumbo
@Gumbo: ahh, I figured as much... like server languages, browser doesn't matter. Thanks again!
Aaron