Forum Moderators: phranque

Message Too Old, No Replies

URL rewriting problems when including a querystring

php_value arg_separator.input ";"

         

fitzer

6:07 pm on Jun 3, 2008 (gmt 0)

10+ Year Member



I have a large mp3 site that is database driven. I have three php pages that display the content for most of the site using URL rewrites in my .htaccess. Some examples:

1) Artist
http://www.example.com/r/the-rolling-stones/

loads
http://www.example.com/rewrites/artist.php?artist=the-rolling-stones

My .htaccess URL Rewrite looks like this:


RewriteRule ^([a-z0]{1})/([.',:()&a-zA-Z0-9-]{2,})/$ rewrites/artist.php?artist=$2&%{QUERY_STRING} [L]

2) Song
http://www.example.com/r/the-rolling-stones/brown-sugar/

loads
http://www.example.com/rewrites/song.php?artist=the-rolling-stones;song=brown-sugar

Note: I'm using a semi-colon as the querystring separator b/c some of the artists or ringtones have an ampersand in the name. This is set in my .htaccess like this:

php_value arg_separator.input ";"

My .htaccess URL Rewrite looks like this:


RewriteRule ^([a-z0]{1})/([.',:()&a-zA-Z0-9-]{2,})/([.',()&a-zA-Z0-9-]{2,})/$ rewrites/song.php?artist=$2;song=$3&%{QUERY_STRING} [L]

The problem:
I'm sending traffic to the site from various ad networks, which pass in querystring values to the URLs. Some examples:

http://www.example.com/r/the-rolling-stones/?keyword=stones
http://www.example.com/r/the-rolling-stones/?keyword=mick%20jagger

I need the URL rewrite to pass the "keyword" value through to the artist.php (and similarly for the song.php process). I can't get the rewrite to work. I would want the above examples to rewrite to this:

http://www.example.com/rewrites/artist.php?artist=the-rolling-stones;keyword=stones
http://www.example.com/rewrites/artist.php?artist=the-rolling-stones;keyword=mick jagger

Any help would be greatly appreciated.

[edited by: jdMorgan at 8:36 pm (utc) on June 3, 2008]
[edit reason] Disabled "smilies" in code [/edit]

jdMorgan

8:37 pm on Jun 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the [QSA] flag for RewriteRule.

Jim

fitzer

6:15 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



Thanks jdMorgan.

QSA flag doesnt seem to help in my case, likely b/c of the custom arg_separator.input I am using.

I'm working with someone on this and will post my results when I sort it out.

fitzer

6:29 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



A friend gave me a solution but it doesn't work.

I *think* I've isolated the problem as being related to the question mark in my rewrite. I'm escaping the question mark but it doesn't seem to work.

Here's a more simplified example:

This URL:
http://www.example.com/r/the-rolling-stones/?123
should rewrite to
http://www.example.com/rewrites/artist.php?artist=the-rolling-stones;keyword=123

I've tried this rewrite with no luck (returns 404):
RewriteRule ^([a-z0]{1})/([.',:()&a-zA-Z0-9-]{2,})/\?([0-9]+)$ rewrites/artist.php?artist=$2;gkw=$3 [L]

I can get this to work:
http://www.example.com/r/the-rolling-stones/123

successfully rewrites to
http://www.example.com/rewrites/artist.php?artist=the-rolling-stones;keyword=123

using this rewrite:
RewriteRule ^([a-z0]{1})/([.',:()&a-zA-Z0-9-]{2,})/([0-9]+)$ rewrites/artist.php?artist=$2;gkw=$3 [L]

Does the question mark have to be escaped in a special way since it is a quantifier?

[edited by: jdMorgan at 11:13 pm (utc) on June 4, 2008]
[edit reason] Disabled graphic smilies in code. [/edit]

wilderness

7:07 pm on Jun 4, 2008 (gmt 0)

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



[webmasterworld.com...]
[webmasterworld.com...]

Search Webmaster World on "question mark" [google.com]

jdMorgan

11:18 pm on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A query string is not part of a URL; It is data attached to a URL to be passed to the resource at that URL. Therefore, query strings are not visible to RewriteRule. You must use a RewriteCond:

/r/the-rolling-stones/?123 --> /rewrites/artist.php?artist=the-rolling-stones;gkw=123


RewriteCond %{QUERY_STRING} ^([0-9]+)$
RewriteRule ^[a-z0]/([.',:()&a-zA-Z0-9\-]{2,})/$ rewrites/artist.php?artist=$1;gkw=%1 [L]

Jim

[edit] Adjusted your rewrite example to match your code [/edit]

[edited by: jdMorgan at 4:22 am (utc) on June 5, 2008]

fitzer

1:42 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



Thanks Jim! You've been an incredible help with this. My rewrites are working perfectly and have been deployed live. Thank you for explaining everything so well and providing the code.

Here are my rewrites for the Artist pages. The first handles the "gkw" value (Google Adwords keyword) in the querystring:

# gkw artist
RewriteCond %{QUERY_STRING} ^gkw=([%.',()a-zA-Z0-9-]+)$
RewriteRule ^([a-z0]{1})/([.',:()&a-zA-Z0-9-]{2,})/$ rewrites/artist.php?artist=$2;gkw=%1 [L]

The second handles "ovkey" (Overture/Yahoo keyword) in the querystring

# ovkey artist
RewriteCond %{QUERY_STRING} ^ovraw=([%.',()a-zA-Z0-9-]+)&ovkey=([%.',()a-zA-Z0-9-]+)&([^/]*)$
RewriteRule ^([a-z0]{1})/([.',:()&a-zA-Z0-9-]{2,})/$ rewrites/artist.php?artist=$2;ovkey=%2 [L]

Thanks again!

[edited by: jdMorgan at 9:49 pm (utc) on June 5, 2008]
[edit reason] Disable graphical smilies in code. [/edit]

jdMorgan

9:53 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that the "[a-z]" subpattern I posted is entirely equivalent to your longer "[a-z]{1}" subpattern, and that there is no need to parenthesize that sub-expression, since you do not back-reference it. Therefore, you can both simplify and speed up your rules by making these changes as suggested in my previous post, and changing $2 to $1 in the substitution.

Jim