Forum Moderators: phranque

Message Too Old, No Replies

Re-write including query

         

spunkymungbeans

3:04 am on Jan 11, 2004 (gmt 0)

10+ Year Member



I have the following rewrite rule which currently works fine:

RewriteRule ^/A(.*) /links/frame.php?action=A&id=$1

This ensures that urls are re-written as follows:

[mysite.com...]
is rewritten as
[mysite.com...]

Some of the pages to which the url is re-directed contain forms, and I would like to change the rule so that it allows this:

[mysite.com...]
rewritten as
[mysite.com...]

Any ideas how I can achieve this? Not all URLS will contain additional queries.

At the moment I do not know what the additional queries might be - they will vary.

SMB

spunkymungbeans

5:52 am on Jan 11, 2004 (gmt 0)

10+ Year Member



I've found a way to make my queries work:

RewriteRule ^/A(.*) /links/frame.php?action=A&id=$1&%{query_string}

This completes the re-write correctly. I had read that I should have put $%[query_string} at the end but this made the query string read:
action=A&id=1?query=2
which meant the final query was just ignored.

Anyway I've discovered that anchors within the returned pages don't work. I was under the impression that anchors worked without referring back to the server. Now I have my forms working again it would be nice for me to get the [top] links working as well. Any ideas?

jdMorgan

5:52 am on Jan 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



spunkymungbeans,

Sure, start with the long ones and work on down to the short ones if the first pattern matches fail due to missing parameters. Something like this:


RewriteCond %{QUERY_STRING} ^query1=(.+)&query2=(.+)$
RewriteRule ^/A([0-9]{3})$ /links/frame.php?action=A&id=$1&query1=%1&query2=%2 [L]
#
RewriteCond %{QUERY_STRING} ^query1=(.+)$
RewriteRule ^/A([0-9]{3})$ /links/frame.php?action=A&id=$1&query1=%1 [L]
#
RewriteRule ^/A([0-9]{3})$ /links/frame.php?action=A&id=$1 [L]

The [L] flag in each rule prevents further mod_rewrite processing if the pattern matches and the URL is updated, so once a longer pattern matches, the rule with the shorter patterns won't do anything. Otherwise, you fall through to the last rule, which takes care of the ones without any pre-existing query string.

The above are written as internal redirects, and assume that the code is in httpd.conf. Remove the leading slash on the RewriteRule patterns for use in .htaccess context. The rules also assume a three-digit number for the ID; you can also set a minimum and maximum acceptable number of digits by specifying min,max, as in [0-9]{1,4}

Jim

<edit> Hah! Cross-posted. My reply is intended for your first post. </edit>