Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule fails when it has a question mark in the string

         

ntbgl

5:24 pm on Aug 18, 2008 (gmt 0)

10+ Year Member



I'm having problems with a rewrite rule in my .htaccess file:

RewriteRule ^feeds/custom\.(rss¦atom)?q=(.*)$/feeds/custom.php?mode=$1&q=$2 [NC]

Normally I'm good at these rewrite rules, but this one isn't working.

If you try to access the file directly, for example: /feeds/custom.php?mode=rss&q=query it works just fine, but /feeds/custom.rss?q=query isn't being rewritten.

I think it might have something to do with ?q= in it, so I've tried something like

RewriteRule ^feeds/custom\.(rss¦atom)\?q=(.*)$/feeds/custom.php?mode=$1&q=$2 [NC]

But that didn't work either.

Any help is much appreciated.

g1smd

7:45 pm on Aug 18, 2008 (gmt 0)

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



There are supposed to be four elements on that line, but I can only see three...

viz. there should be both a URL path and a server path with a space between them.

jdMorgan

9:34 pm on Aug 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^feeds/custom\.(rss¦atom)$ /feeds/custom.php?mode=$1&q=%1 [NC]

Query strings are not directly visible to RewriteRule, since they are not part of the URL, but rather, data attached to the URL to be passed to the resource at that URL.

More information is available in the on-line Apache mod_rewrite documentation.

Change the broken pipe "¦" character above to a solid pipe character before use; Posting on this forum modifies the pipe characters.

Jim

ntbgl

9:50 pm on Aug 18, 2008 (gmt 0)

10+ Year Member



Thank you very much, the RewriteCond was exactly what I needed.

I had no idea that RewriteRule didn't affect query strings, this is the first time I ever used them with that. I thought my error had something to do with not escaping the question mark or the equals sign.

As far as my top post being three parts, not four, apparently my tab in between the two middle parts got stripped out when I posted.

Thanks again everyone!