mod_rewrite and erasing (and passing) query string

mod_rewrite and erasing (and passing) query string

am 15.06.2007 19:36:19 von iwtfcf

Is it possible to use mod_rewrite to erase the query string and
simultaneously pass it to an underlying script for processing? For
example, a browser requests this URL:

http://www.somewhere.com/thescript/thepage.html?param1=value 1¶m2=value2

I'd like the browser location bar to display just the REQUEST_URI:

http://www.somewhere.com/thescript/thepage.html

But at the same time the query string will have been passed to
"thescript" to do whatever it needs to do with those parameters.

I know I can do something like this to erase the query string:

RewriteCond %{QUERY_STRING} (^param1)
RewriteRule (.*) %{REQUEST_URI}

But then the query string is not available for processing by
"thescript".

Re: mod_rewrite and erasing (and passing) query string

am 15.06.2007 20:02:00 von iwtfcf

On Jun 15, 1:36 pm, iwt...@yahoo.com wrote:
> Is it possible to use mod_rewrite to erase the query string and
> simultaneously pass it to an underlying script for processing? For
> example, a browser requests this URL:
>
> http://www.somewhere.com/thescript/thepage.html?param1=value 1¶m2=...
>
> I'd like the browser location bar to display just the REQUEST_URI:
>
> http://www.somewhere.com/thescript/thepage.html
>
> But at the same time the query string will have been passed to
> "thescript" to do whatever it needs to do with those parameters.
>
> I know I can do something like this to erase the query string:
>
> RewriteCond %{QUERY_STRING} (^param1)
> RewriteRule (.*) %{REQUEST_URI}
>
> But then the query string is not available for processing by
> "thescript".

Just forgot to add a couple of things to the RewriteRule above. It
should read:
RewriteRule (.*) %{REQUEST_URI}? [R]

Re: mod_rewrite and erasing (and passing) query string

am 15.06.2007 21:01:35 von Joshua Slive

On Jun 15, 1:36 pm, iwt...@yahoo.com wrote:
> Is it possible to use mod_rewrite to erase the query string and
> simultaneously pass it to an underlying script for processing? For
> example, a browser requests this URL:
>
> http://www.somewhere.com/thescript/thepage.html?param1=value 1¶m2=...
>
> I'd like the browser location bar to display just the REQUEST_URI:
>
> http://www.somewhere.com/thescript/thepage.html
>
> But at the same time the query string will have been passed to
> "thescript" to do whatever it needs to do with those parameters.
>
> I know I can do something like this to erase the query string:
>
> RewriteCond %{QUERY_STRING} (^param1)
> RewriteRule (.*) %{REQUEST_URI}
>
> But then the query string is not available for processing by
> "thescript".

No, you can't really do that.

In order to change what appears in the browser's URL-bar, you need to
do an external redirect (using the [R] flag to RewriteRule), which
asks the browser to make a new request. But when using HTTP, each
request is entirely independent, so there would be no way for apache
to know that the new request should use the query parameters from the
old request.

You can do something tricky like shoving the query string into a
cookie, which the browser should send on subsequent requests. But I
really wouldn't recommend it. This will break the usefulness of the
URL for bookmarking, sending links, etc, because the URL alone will no
longer be enough to fully describe the page that should be delivered.

Joshua.