Forum Moderators: phranque

Message Too Old, No Replies

Mod-rewrite trouble.

Server can't handle real URL code before the $?

         

Jesse_Smith

7:13 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to allow real URL code before the $ ?

Working URL and code (URL part that changes is shown as ZZZ)....

cgi-local-file.cgi-myOperation=Used&ItemId=ZZZ/Page/ZZZ

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^cgi-local-fille.cgi-myOperation=Used&ItemId=(.*)/Page/(.*)$ cgi-local/file.cgi?myOperation=Used&ItemId=$1&ItemPage=$2 [L]

Non working URL that I'm trying to get to work, the code, and error message....

cgi-local/file.cgi?myOperation=Used&ItemId=ZZZ/Page/ZZZ

RewriteRule ^cgi-local/file.cgi?myOperation=Used&ItemId=(.*)/Page/(.*)$ cgi-local/file.cgi?myOperation=Used&ItemId=$1&ItemPage=$2 [L]

Error message on page: ZZZ/Page/ZZZ is not a valid value for ItemId. Please change this value and retry your request.

The part that is messing it up is having

cgi-local/file.cgi?

on the left before the $

The / and ? mess it up.

How do I get this to work? I even tried having a \ before both the / and ? .

Trying

RewriteRule ^cgi-local(.*)amazon_products_feed.cgi(.*)myOperation=Used&ItemId=(.*)/Page/(.*)$ cgi-local$1amazon_products_feed.cgi$2myOperation=Used&ItemId=$3&ItemPage=$4 [L]

and

RewriteRule ^(.*)myOperation=Used&ItemId=(.*)/Page/(.*)$ $1myOperation=Used&ItemId=$3&ItemPage=$4 [L]

also didn't make it work. I also tried

RewriteRule ^cgi-local(.*)file.cgi(.*)$ cgi-local$1file.cgi$2 [L]

but that's a loop that suddenly hogs about half the servers CPU, and I had to kill it in SSH top. (Stop it in the browser and it keeps going.)

jdMorgan

4:02 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule cannot "see" query strings, such as "?ItemID=" appended to URLs. This is because query strings are not part of a URL (as defined), they are data parameters to be passed to a resource *at* that URL.

In order to test query strings in mod_rewrite, you can use


RewriteCond %{QUERY_STRING} ^ItemID=(ID_here)$

and back-reference the matched ItemID value by the use of %1 in the following RewriteRule.

Jim

Jesse_Smith

6:35 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nothing. I even tried placing the actual ID there

RewriteCond %{QUERY_STRING} ^ItemID=B000059H9A$
RewriteRule ^cgi-local/file.cgi?myOperation=Used&ItemId=(.*)/Page/(.*)$ cgi-local/file.cgi?myOperation=Used&ItemId=%1&ItemPage=$2 [L]

It's not that part that's messing it up, but the

cgi-local/file.cgi?

part. The / and ? mess it up.

cgi-local-file.cgi-myOperation=Used&ItemId=ZZZ/Page/ZZZ
and
RewriteRule ^cgi-local-file.cgi-myOperation=Used&ItemId=(.*)/Page/(.*)$ cgi-local/file.cgi?myOperation=Used&ItemId=$1&ItemPage=$2 [L]

do work. Notice the two -'s instead of / and ?

jdMorgan

7:16 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You shouldn't do this:

RewriteRule ^cgi-local-file.cgi-myOperation=Used&ItemId=(.*)/Page/(.*)$ cgi-local/file.cgi?myOperation=Used&ItemId=$1&ItemPage=$2 [L]

Because you have no "?" in the requested URL to delimit the querystring, and the resulting URL is invalid because it contains reserved characters in the resource locator.

RewriteRule ^cgi-local/file.cgi?myOperation=Used&ItemId=(.*)/Page/(.*)$ cgi-local/file.cgi?myOperation=Used&ItemId=$1&ItemPage=$2 [L]

is more properly written as:

RewriteCond %{QUERY_STRING} ^ItemId=([^/]*)/Page/(.*)
RewriteRule ^cgi-local/file.cgi$ /cgi-local/file.cgi?myOperation=Used&ItemId=%1&ItemPage=%2 [L]

Note that "?" has a special meaning in regular expressions patterns. It makes the preceding character or parenthesized group of characters optional. If you wish to match a literal "?" in a regular expressions patterns, it must be escaped by preceding it with "\".

Jim

Jesse_Smith

7:57 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Same error. Here's a shorter example that does the exact same thing.

This works.

RewriteRule ^cgi-local-file.cgi-test$ /cgi-local/file.cgi?real

domain.com/cgi-local-file.cgi-test

But this doesn't.

RewriteRule ^cgi-local/file.cgi?test2$ /cgi-local/file.cgi?real

domain.com/cgi-local/file.cgi?test2

What would I change to get this one to work?

jdMorgan

8:01 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remove the query string from RewriteRule where it cannot match, and put it in a RewriteCond, as stated above.

RewriteCond %{QUERY_STRING} ^test2$
RewriteRule ^cgi-local/file\.cgi$ /cgi-local/file.cgi?real [L]

Jim