Forum Moderators: phranque

Message Too Old, No Replies

Last bit for static->dynamic mapping

Can't handle the base directory

         

StuffOfInterest

1:58 pm on Apr 13, 2005 (gmt 0)

10+ Year Member



I've been working on a website with a large, and growing, amount of dynamic content. It will present much better if I can reform it into a browsable tree with a more static appearance. I think I have the mod_rewrite rule 90% there with the following bit of code:


RewriteEngine on
RewriteRule ^handler\.php$ - [L]
RewriteRule ^(.+)$ /Redirect/handler.php?cat=$1&%{QUERY_STRING} [L]

This works for the following situations:


/Redirect/test1/test2.html -> /Redirect/handler.php?cat=test1/test2.html
/Redirect/test1/test2.html?a=b&c=d -> /Redirect/handler.php?cat=test1/test2.html&a=b&c=d
/Redirect/test1?a=b&c=d -> /Redirect/handler.php?cat=test1&a=b&c=d

Unfortunately, it breaks on this scenario:


/Redirect/?a=b&c=d

Apparently, queries based on the root directory for the redirect don't map over correctly.

Can anyone clue-by-four me with the last bit of the rule I'm missing?

Thanks.

jdMorgan

2:33 pm on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This rule requires that the requested local URL path be non-blank, because the pattern ".+" means, "one or more of any character."

RewriteRule ^(.+)$ /Redirect/handler.php?cat=$1&%{QUERY_STRING} [L]

In the case of a request for "/", the localized URL-path seen by RewriteRule in .htaccess will be blank, so the rule fails.

I'd suggest:


RewriteRule ^(.*)$ /Redirect/handler.php?cat=$1 [QSA,L]

Jim

StuffOfInterest

3:03 pm on Apr 13, 2005 (gmt 0)

10+ Year Member



Oh, what a difference a single letter can make! Yes, that made it start processing for the root directory. Now, with a string like "/Redirect/?a=b", the "cat" parameter ends up empty which is easy to test for. One odd side effect is if the trailing slash is left off, as in "/Redirect?a=b". In this situation the entire physical file path through "Redirect" gets put in as "cat". Ugly, but that can be detected and handled.

One more stupid programmer question. What is the "QSL" param for?

Thanks for the help with this.

jdMorgan

3:09 pm on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



QSA = Query String Append -- just a simpler and easier-to-read way to append the pre-existing query string.

Jim

StuffOfInterest

3:53 pm on Apr 13, 2005 (gmt 0)

10+ Year Member



QSA...QSL....It's all starting to sound like HAM radio speak to me. :)

OK, these rules ended up being much shorter than I would have expected.


RewriteEngine on
RewriteRule ^handler\.php$ - [L]
RewriteRule ^(.*)$ handler.php?cat=$1 [L,QSA]

Note that even the "/Redirect" wasn't necessary being that handler.php is in the Redirect directory. Except for the slightly odd behavior when no trailing slash is present on "/Redirect" everything looks great. Fortunately, that is a pretty easy test to put in the code.

Thanks very much for the help.