Forum Moderators: coopster

Message Too Old, No Replies

Odd behavior from $_SERVER and mod_rewrite

Rewritten query string does not appear in [$_SERVER]=>QUERY_STRING

         

dnellis74

9:54 pm on Oct 21, 2004 (gmt 0)

10+ Year Member



I already tried the fellows in the Apache forum, but Im thinking this is more of a quirk in PHP than Apache

Searching this forum I found great help to generating rewrite rules to make a dynamic request appear as static search engine friendly URLs. For example :?cat1=foo&cat2 => bar /foo/bar.

My php seems like its being ornery.

Here are my rewrite rules. This is inside of a directory directive:

RewriteRule ^([^/]+)/([^/]+) index.php?state=$1&city=$2 [L]
RewriteRule ^([^/]+) index.php?state=$1 [L]

Now after accessing somehting like [mysite.com...] in PHP when I print out the $_SERVER variable I get two interesting bits of output:

[REDIRECT_QUERY_STRING] => state=AK
[QUERY_STRING] => state=index.php

How on Earth did state get assigned to be index.php in QUERY_STRING? Its not in the original query at all. Do you think some other directive higher up in the site is fiddling with things? I think REDRIECT_QUERY_STRING is an apache variable. Did apache work correctly and PHP is goofing up?

Thanks in advance if you can straighten me out. I am pretty stumped.

dmorison

7:37 pm on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As a stab in the dark in the absense of any better answer; i'd try putting the regexp end-of-line character ($) on the end of your re-write rules....

RewriteRule ^([^/]+)/([^/]+)$ index.php?state=$1&city=$2 [L]

dnellis74

7:51 pm on Oct 22, 2004 (gmt 0)

10+ Year Member



Thanks.

I made a small bit of progress on this since I posted, and I'm pretty sure that is this an Apache issue and not a PHP issue.

When I print_r $_SERVER I get two interesting bits output:

[REDIRECT_QUERY_STRING] => state=AK
[QUERY_STRING] => state=index.php

So the redirect string is correct, and the query string is pretty bizarre. This is definately a question for an Apache expert, but maybe a PHP person will be able to shed some light. Not to mention they are all Apache experts too :)

dnellis74

7:53 pm on Oct 22, 2004 (gmt 0)

10+ Year Member



Argh! I should have reread my original post!

Sorry for being repetitive.

ergophobe

4:54 pm on Oct 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ^([^/]+)/([^/]+) index.php?state=$1&city=$2 [L]
RewriteRule ^([^/]+) index.php?state=$1 [L]

It looks like it's not stopping at the [L] flag when it has a match (also, your regex doesn't match correctly as you'll see). So you seem to be having something like this happening...

directory/AK (this matches the first rule and yields:)

=>index.php?state=directory&city=AK (this matches the second rule and yields:)

=>index.php?state=index.php?state=directory&city=AK (the second? and after gets thrown away:)

=>index.php?state=index.php

Try putting this in as your first rewrite rule

RewriteRule ^(.*)index.php(.)$ - [L]

As for the other rule, you need something that takes into account the "directory" term and throws it away or uses it somehow.