Forum Moderators: phranque

Message Too Old, No Replies

Simple mod rewrite gone wrong

Can't pass the correct variable through a mod rewrite of files name

         

devinrayolsen

1:54 am on Apr 12, 2009 (gmt 0)

10+ Year Member



My question is with this guy:
RewriteRule ^(.*)\.php$ index.php?pageName=$1 [L]
of even this guy:
RewriteRule ^([^/]+)\.php index.php?pageName=$1 [NC]

Upon using it on a url like this:
[localhost...]

The pageName variable when reached by index.php comes out as "index" instead of the requested "home".

Can anyone shed some light why when passing home.php into the pageName variable it comes out as "index" on the other end?

Running Apache 2.2 on windows.

devinrayolsen

2:17 am on Apr 12, 2009 (gmt 0)

10+ Year Member



Wow, few minutes of searching on other people posts and I found a guy having problems similar to mine. Solution, QSA flag instead of L...

Anyone mind explaining to me exactly what a QSA did for me here?

jdMorgan

3:41 am on Apr 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It put a band-aid on the basic problem that your rule rewrites its own output URL-path recursively -- because the pattern matches the result of the rewrite. Examine your server error log to see the "Maximum internal redirection limit reached -- assuming server misconfiguration error" message that is likely in there.

The first time through, the pageName variable is assigned the value "home" and the URL is rewritten to "index.php". But the rule runs again, because after any rule is invoked in an .htaccess context, mod_rewrite processing is re-started. So, the second time through, the "php page name" is now index.php, and this is populated into the query string pageName value. Then the rule runs again, and repeats until the server gives up.

Using [QSA], you'll likely find that your actual query string was
"pageName=home&pageName=index&pageName=index&pageName=index&pageName=index&pageName=index&pageName=index&pageName=index&pageName=index&pageName=index"
because QSA (Query String Append) tells mod_rewrite to append the new query parameters to the existing ones, rather than replacing the entire query string. (I show ten pageName= parameters, assuming that your server redirection limit is set to ten. You may see more or less depending on your actual limit setting, but you'll see only one set to "home" and the rest set to "index.")

The correct solution is to explicitly prevent recursion:


RewriteCond $1 !^index$
RewriteRule ^([^.]+)\.php$ index.php?pageName=$1 [L]

Jim

devinrayolsen

8:46 am on Apr 12, 2009 (gmt 0)

10+ Year Member



Thank you very much, simple as that!

Recursion sucks, band aids will hurt and your solution fixes both.

Studied, noted and saved.

Thanks again jdMorgan!