Forum Moderators: phranque

Message Too Old, No Replies

Problem with "?" in mod rewrite

strange problem with escaping

         

wizardone

2:39 pm on Aug 26, 2009 (gmt 0)

10+ Year Member



Hi guys, i have a strange problem and i hope that someone can help me out.
I have the following short url that is being generated by another program - 'system_name/?SessionID=bla-bla' and i want to mod rewrite to 'php/admin.php?system=system_name&sessionID=bla-bla'

The .htaccess file looks like this:
RewriteEngine on
RewriteRule ^([^"]+)/?SessionID=([^"]+)$ php/admin.php?system=$1&SessionID=$2 [L,NC]

And it`s not working(error 404 not found).
If i remove the "?" before SessionID everything is fine and works perfect. I tried to escape the "?" with various regular expressions, but nothing seems to work. Any ideas?

jdMorgan

2:51 pm on Aug 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mod_rewrite's RewriteRule cannot 'see' query strings attached to URLs. You have to use a RewriteCond if you wish to test and/or back-reference a query string.

Further, you can use [QSA] to append additional query parameters while keeping the original ones, making it unnecessary to 'manually copy' anything from the original query string, unless -as in this case- you want to do case-conversion or otherwise change the parameter name.


RewriteCond %{QUERY_STRING} ^SessionID=([^"]+)$ [NC]
RewriteRule ^([^"]+)/$ php/admin.php?system=$1&SessionID=%1 [L]

It is not clear why your patterns uses a negative match for a double quote, so I copied that without change in both the URL-path pattern and the query string pattern. It's likely not needed in both, and might be better changed to something more or less specific.

The resources cited in our Forum Charter, particularly the mod_rewrite documentation, might prove useful.

Jim

wizardone

6:05 am on Aug 27, 2009 (gmt 0)

10+ Year Member



Thanks for the reply :) It helped me a lot and it works now.