|
Home > Archive > Apache Server configuration support > January 2007 > Problems with Query string for ReWriteRule.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problems with Query string for ReWriteRule.
|
|
| kevytan@gmail.com 2007-01-25, 1:39 am |
| I have a following url that needs to be written so it can point to a
new website.
For example I have a domain xyz.com. The file that needs to be
re-written is
http://www.xyz.com/sample.jtml?CATID=813&PRODID=200
I'm not necessarily trying to capture the query string, that is the
information after the "?" mark. In my .htaccess file i have something
like this.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)CATID=813(.*)$ http://www.google.com.au
For example I am trying to capture the CATID=813 and forward it to
google's website. When Apache intepret's it, anything after the "?"
mark is lost, therefore it cannot be evaluated. Is there a way to
evaluate the whole string, evem the text after the question mark?
Thanks.
| |
| Jim Hayter 2007-01-25, 1:39 am |
| kevytan@gmail.com wrote:
> I have a following url that needs to be written so it can point to a
> new website.
>
> For example I have a domain xyz.com. The file that needs to be
> re-written is
>
> http://www.xyz.com/sample.jtml?CATID=813&PRODID=200
>
> I'm not necessarily trying to capture the query string, that is the
> information after the "?" mark. In my .htaccess file i have something
> like this.
>
> Options +FollowSymlinks
> RewriteEngine on
> RewriteBase /
> RewriteRule ^(.*)CATID=813(.*)$ http://www.google.com.au
>
> For example I am trying to capture the CATID=813 and forward it to
> google's website. When Apache intepret's it, anything after the "?"
> mark is lost, therefore it cannot be evaluated. Is there a way to
> evaluate the whole string, evem the text after the question mark?
>
> Thanks.
>
Try this (untested)
RewriteEngine on
RewriteCond %{QUERY_STRING} ^CATID=813(.*)$
RewriteRule ^(.*)$ http://www.google.com.au
I don't know why you are using the parentheses in your RewriteRule.
They are used to capture parts of the expression to use in the rewrite
rule result. In your rewrite rule, the first expression would be
referenced as $1 and the second as $2 when rewriting. Since you don't
use them, you don't need the parentheses. I kept them in my example
above, thinking you might need them. In my example, the parenthesized
expression in the condition would be referenced as %1 and the one in the
RewriteRule as $1. If you won't be using them, drop the parens.
HTH,
Jim
| |
| kevytan@gmail.com 2007-01-25, 1:39 am |
| Thanks Jim,
(.*) in the front of the rewriterule, as sometimes there maybe a
session id before proceeding to the CATID. I tried the code that you
provided and it works, it redirects it to google.com.au. The thing now
is that the query string gets attached to the end of the google url.
Is there a way not to include it in there?
On Jan 25, 4:01 pm, Jim Hayter <see.reply...@nowhere.invalid> wrote:
> kevy...@gmail.com wrote:
>
>
>
>
>
>
>
> RewriteEngine on
> RewriteCond %{QUERY_STRING} ^CATID=813(.*)$
> RewriteRule ^(.*)$http://www.google.com.au
>
> I don't know why you are using the parentheses in your RewriteRule.
> They are used to capture parts of the expression to use in the rewrite
> rule result. In your rewrite rule, the first expression would be
> referenced as $1 and the second as $2 when rewriting. Since you don't
> use them, you don't need the parentheses. I kept them in my example
> above, thinking you might need them. In my example, the parenthesized
> expression in the condition would be referenced as %1 and the one in the
> RewriteRule as $1. If you won't be using them, drop the parens.
>
> HTH,
> Jim- Hide quoted text -- Show quoted text -
| |
| Jim Hayter 2007-01-25, 1:21 pm |
| kevytan@gmail.com wrote:
> Thanks Jim,
>
> (.*) in the front of the rewriterule, as sometimes there maybe a
> session id before proceeding to the CATID. I tried the code that you
> provided and it works, it redirects it to google.com.au. The thing now
> is that the query string gets attached to the end of the google url.
> Is there a way not to include it in there?
>
\
My point was that you don't need the parens around the .*.
The rule could be:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^CATID=813.*$
RewriteRule .* $http://www.google.com.au
You might try changing the rewrite rule to:
RewriteRule .* $http://www.google.com.au?
When the rewrite rule has a query string (null query string in the
above), the original query string is dropped unless the QSA flag is
specified. I think the addition of the '?' above will cause the
original query string to be dropped.
| |
|
| "Jim Hayter" <see.reply.to@nowhere.invalid> schreef in bericht
news:12rhlsl8miklm45@news.supernews.com...
> kevytan@gmail.com wrote:
> The rule could be:
>
> RewriteEngine on
> RewriteCond %{QUERY_STRING} ^CATID=813.*$
> RewriteRule .* $http://www.google.com.au
>
Make the condition accept the element to be anywhere on the line, either
preceded or preceding eg the sessionid -the order of elements is not
guaranteed to match the order of occurrance in the form-
RewriteCond %{QUERY_STRING} (.*&)?CATID=813(.*&)
The alternative seperater ; -replacing both ? and & is NOT understood by
Apache. It may cause Apache to croak 404 on urls like
www.some.where/this/is_a.file;jsession=123456.
> You might try changing the rewrite rule to:
> RewriteRule .* $http://www.google.com.au?
The $ upfront feels funny, you shoud do without.
> When the rewrite rule has a query string (null query string in the above),
> the original query string is dropped unless the QSA flag is specified.
Be aware data POSTed will be lost anyway and CMIIW cann't be used in a
condition.
HansH
| |
| Jim Hayter 2007-01-26, 1:20 pm |
| HansH wrote:
> "Jim Hayter" <see.reply.to@nowhere.invalid> schreef in bericht
> news:12rhlsl8miklm45@news.supernews.com...
> Make the condition accept the element to be anywhere on the line, either
> preceded or preceding eg the sessionid -the order of elements is not
> guaranteed to match the order of occurrance in the form-
> RewriteCond %{QUERY_STRING} (.*&)?CATID=813(.*&)
>
> The alternative seperater ; -replacing both ? and & is NOT understood by
> Apache. It may cause Apache to croak 404 on urls like
> www.some.where/this/is_a.file;jsession=123456.
Interesting, is ; commonly used as a separator?
> The $ upfront feels funny, you shoud do without.
Yeah, a typo.
> Be aware data POSTed will be lost anyway and CMIIW cann't be used in a
> condition.
Good point.
Jim
| |
| shimmyshack 2007-01-26, 1:20 pm |
|
On Jan 26, 5:45 pm, Jim Hayter <see.reply...@nowhere.invalid> wrote:
> HansH wrote:
>
>
>
>
>
>
> Jim
If you were using this rule in conjuction with a simple PERL or php
script you could rewrite all requests to index.php which could take the
whole request (including post, get cookies) and post the data to the
new site, where another script could catch it, to do it you would just
need a few lines using the cURL library.
The thing would work transparently then, passing all those 010's across
the infinitudes of the net.
| |
|
|
| Jim Hayter 2007-01-29, 1:18 pm |
| HansH wrote:
<several references to the use of ';' as a separator in URLs>
Interesting reading.
Thank you.
|
|
|
|
|