Forum Moderators: phranque

Message Too Old, No Replies

.htaccess catching query string after .html

Need to preserve original query

         

xtremewalls

2:37 pm on Aug 28, 2009 (gmt 0)

10+ Year Member



Hi guys,
this is my current rewrite rule:
RewriteRule ^games/(.*)\.html$ games/game.php?games=$1 [L]
its working absolutely fine. it sends the value to the right page. but i cannot seem to modify it so that if i get a url request like say "http://www.example.com/abcd.html?ref=123" it send the "ref" value to the php page too.
can you please tell me how to send the ref value to the php script too...
Thanks a lot in advance..

jdMorgan

3:29 pm on Aug 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the Apache mod_rewrite documentation [httpd.apache.org], and look at the [QSA] flag under the RewriteRule section.

Jim

xtremewalls

3:33 pm on Aug 28, 2009 (gmt 0)

10+ Year Member



sorry.. i forgot to mention that i don't understand much part of it.. so if you know the solution kindly guide me.
thanks

wilderness

3:54 pm on Aug 28, 2009 (gmt 0)

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



Your a newcomer here (Welcome) the Charter of this forum is not to provide the functioning lines for you, rather to allow you the ability to grasp working methods.

Jim and others do a marvelous job here and all as volunteers (without compensation).

Thus. . .show your best effort and ask for comments and assistance from that effort.

jdMorgan

3:55 pm on Aug 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should not use tools that you don't understand. Doing so can be very dangerous to your site.

RewriteRule ^games/(.+)\.html$ games/game.php?games=$1 [QSA,L]

Jim

wilderness

3:56 pm on Aug 28, 2009 (gmt 0)

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



a search on [QSA]at Webmaster World [google.com]

xtremewalls

4:21 pm on Aug 28, 2009 (gmt 0)

10+ Year Member



thank you so much guys... i did not mean to offend anybody and if i did unknowingly then i apologize.. i'll try understanding QSA

xtremewalls

4:42 pm on Aug 28, 2009 (gmt 0)

10+ Year Member



ok.. so after much effor i came up with thi:
RewriteRule ^games/(.*)\.html\\?ref=(.*)$ games/game.php?games=$1&ref=$2 [QSA,L]

if i remove the "\\?ref=" part then it works fine but then the url looks like this: "something.htmlabcd"
is there another way the "?" should be matched?

jdMorgan

5:03 pm on Aug 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rule, if it had been properly implemented, would have done almost exactly what the code I posted above does, except that it would have been much less efficient.

That malformed "something.htmlabcd" URL is NOT this mod_rewrite rule's doing...

With the rule I posted above,

 RewriteRule ^games/(.+)\.html$ games/game.php?games=$1 [QSA,L] 

a request for
example.com/games/whack-a-mole.html
gets passed to your script as
example.com/games/game.php?games=whack-a-mole

and a request for
example.com/games/whack-a-mole.html?level=3
gets passed to your script as
example.com/games/game.php?level=3&games=whack-a-mole

If anything else is happening, then the problem is likely either another rewriterule interfering with this one, or the game.php script itself. The reason I can say that is that neither your original rule nor this new one can "change the URL" because both of them are internal rewrites. Neither of them communicates in any way with the browser, and so cannot change the URL in the browser's address bar.

In order to change the address in the browser's address bar, an external redirect is required (as opposed to this internal rewrite). So this indicates that another mechanism is coming into play, and generating an external redirect response to the browser.

When reporting problems, please include both the code (if changed) and the URL you used to test. Also, please be sure that your example URLs remain consistent. Otherwise, debugging even trivial code such as this will become a chore.

Jim

xtremewalls

5:22 pm on Aug 28, 2009 (gmt 0)

10+ Year Member



thanks a million Jim! this is my first day on the forum and i feel so welcomed already... i just hope that i'll continue to learn something new on this forum everyday!

jdMorgan

5:42 pm on Aug 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, be sure to completely flush (delete) your browser cache after changing any server-side code. Otherwise, your browser is likely to show you previously-cached pages and server responses, which can seriously confuse things. :o

Jim

jdMorgan

5:49 pm on Aug 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I should also mention that without [QSA] --Query String Append-- your new query string *replaces* the originally-requested query string. [QSA] simply states that you want the RewriteRule to *append* additional parameters to that existing query string.

In the absence of a new query string and [QSA], mod_rewrite passes query strings through rewrites unchanged by default.

Also note that RewriteRule cannot 'see' query strings appended to requested URL-paths, as they are data *attached to a URL* to be passed to the resource *at* that URL. For this reason, you must use a RewriteCond to examine %{QUERY_STRING} if you wish to match and back-reference query strings. However, that's not necessary in this case, as using [QSA] seems entirely sufficient.

Oh, and to make it official -- Welcome to WebmasterWorld!

Jim