views:

112

answers:

3

I have a strange problem with mod_rewrite, the rules that are relevant here are:

RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]

And a corresponding URL might look something like this:

example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php

The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used. So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?

ps: other rules in the .htaccess file are working fine...

A: 

mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:

RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)

Ah, like Gumbo said; you can also use %1 to back reference to the page numer.

bouke
+2  A: 

The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.

RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]


But you can still simplify this by using the QSA flag (query string append):

RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
Gumbo
I didn't know about that. I guess I never used a rewrite that would rewrite the query part.Thank you very much for your ultra quick solution!
Jan Hancic
A: 

Is it just me or are your arguments back-to-front?

Do you mean:

RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]

You wanted to match miselne-igre not igre-miselne.

Obviously this doesn't address the main issue, but thought I'd throw that in.

Dom

Dominic Rodger
In the given example I'm only interested in the "shirk" part. So my rewrite is OK regarding that :)
Jan Hancic
I also got caught by that at first, but he doesn't catch the path, but the filename and the slash at it's beginning ("/igre"), slash has to be escaped, so it is '\/igre'
schnaader
ok - my mistake - sorry!
Dominic Rodger