Quantcast

Forums | MacLife

You are not logged in.

#1 2007-04-17 11:28 am

GreenAlge
The Algae
From: Who knows?
Registered: 2003-09-03
Posts: 802
Website

mod_rewrite loses post data!

So, I've been using mod_rewrite with my site, and it's been working beautifully until recently... now the POST DATA gets dropped somewhere along the way!

Here's my .htaccess file

Code:

RewriteEngine on

RewriteCond %{QUERY_STRING} (.*)
RewriteRule pages/(.*) ./?page=$1&%1

Any solutions?

Thanks!
_Nik

Offline

 

#2 2007-04-17 1:20 pm

Rozzlapeed
Born to be IT
From: Scottsdale, AZ
Registered: 2003-01-02
Posts: 1095
Website

Re: mod_rewrite loses post data!

That shouldn't happen. Are you processing the post data with PHP?


"He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife."
-- Douglas Adams, The Hitch Hiker's Guide to the Galaxy

Offline

 

#3 2007-04-17 1:56 pm

GreenAlge
The Algae
From: Who knows?
Registered: 2003-09-03
Posts: 802
Website

Re: mod_rewrite loses post data!

Yes, using the $_POST variable. Like I said, the exact same files used to work fine (I think Apache got updated to v2.x) and now they don't. If I don't use rewrite, everything works fine, if I do, there's no POST data whatsoever.

_Nik

Offline

 

#4 2007-04-18 12:39 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: mod_rewrite loses post data!

Sounds like mod_rewrite is performing a redirect in which case post data will get dumped.

I'm a bit confused as to what you're trying to accomplish with those rewrite rules.

Offline

 

#5 2007-04-18 12:46 am

GreenAlge
The Algae
From: Who knows?
Registered: 2003-09-03
Posts: 802
Website

Re: mod_rewrite loses post data!

Gipetto wrote:

I'm a bit confused as to what you're trying to accomplish with those rewrite rules.

Nicer urls. e.g., domain.com/?page=login becomes domain.com/pages/login. Easier to track hits with AWStats too.

_Nik

Offline

 

#6 2007-04-18 12:53 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: mod_rewrite loses post data!

I get that, but your first condition looks like you set a condition on the presence of a query string, but the rule takes a 'pretty' url and transforms it to a query string... seems contradictory.

But, if your user submits to /page.php?a=b and the site redirects then, yes, absolutely you will lose your post data.

Every url, link, src and href on your site should be "pretty" and the transformation in mod_rewrite should be for the server only.

Last edited by Gipetto (2007-04-18 12:59 am)

Offline

 

#7 2007-04-18 1:00 am

GreenAlge
The Algae
From: Who knows?
Registered: 2003-09-03
Posts: 802
Website

Re: mod_rewrite loses post data!

Well, the query string condition is there so that I can still use variables from there when needed (often in this particular site). So, you'll see something like /pages/login?callback=blah which redirects to /?page=login&callback=blah

And here's the thing... it worked perfectly. Something changed on the server (not sure what, I think there may have been an upgrade to Apache v2.0) that broke it; the exact same setup works on my local machine too.

_Nik

Offline

 

#8 2007-04-18 1:01 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: mod_rewrite loses post data!

I'm gonna wager that when it works you're hitting a weird loophole or bad implementation.

Offline

 

#9 2007-04-18 1:03 am

GreenAlge
The Algae
From: Who knows?
Registered: 2003-09-03
Posts: 802
Website

Re: mod_rewrite loses post data!

So... there's no way to keep POST data with a URL rewrite?

_Nik

Offline

 

#10 2007-04-18 1:40 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: mod_rewrite loses post data!

POST data can be retained across mod_rewrite transformations - I just think your rules are wrong.
For example I do:

Code:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

Which simply states that any request that doesn't resolve to a real file or directory comes back to index.php - This is basically every request to the site. I don't lose any post data.

I still think you're getting an 'external redirect' vs. an 'internal redirect' in which case your POST is getting dropped. It might be the double transformation, but I'm not sure.

Offline

 

#11 2007-04-18 1:55 am

GreenAlge
The Algae
From: Who knows?
Registered: 2003-09-03
Posts: 802
Website

Re: mod_rewrite loses post data!

Wouldn't that loose the query string though? That's what was happening before I started using the RewriteCond

_Nik

Offline

 

#12 2007-04-18 9:18 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: mod_rewrite loses post data!

Nope, I don't lose GET or POST.
mod_rewrite automatically handles GET separately, so whatever you do to the url the get string will be appended to it automatically after the transformation.

Offline

 

#13 2007-04-18 9:34 am

Rozzlapeed
Born to be IT
From: Scottsdale, AZ
Registered: 2003-01-02
Posts: 1095
Website

Re: mod_rewrite loses post data!

GreenAlge wrote:

So, I've been using mod_rewrite with my site, and it's been working beautifully until recently... now the POST DATA gets dropped somewhere along the way!

Here's my .htaccess file

Code:

RewriteEngine on

RewriteCond %{QUERY_STRING} (.*)
RewriteRule pages/(.*) ./?page=$1&%1

Any solutions?

Thanks!
_Nik

How about this:

Code:

RewriteEngine on
RewriteRule pages/(.*) ./?page=$1&%{QUERY_STRING}

Last edited by Rozzlapeed (2007-04-18 9:36 am)


"He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife."
-- Douglas Adams, The Hitch Hiker's Guide to the Galaxy

Offline

 

#14 2007-04-18 9:36 am

Rozzlapeed
Born to be IT
From: Scottsdale, AZ
Registered: 2003-01-02
Posts: 1095
Website

Re: mod_rewrite loses post data!

Gipetto wrote:

Nope, I don't lose GET or POST.
mod_rewrite automatically handles GET separately, so whatever you do to the url the get string will be appended to it automatically after the transformation.

When I was playing around with something else I was doing, it was losing the GET variables on rewrite. I just appended them manually. I think there might also be a flag called qsappend.


"He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife."
-- Douglas Adams, The Hitch Hiker's Guide to the Galaxy

Offline

 

#15 2007-04-18 11:24 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: mod_rewrite loses post data!

No, we're doing a redirect on "that other forum" that transparently pushes the GET string to the new url. We also don't define any actions for the GET portion, so maybe by leaving GET alone in all respects allows Apache to take care of it by itself. If you need to take a url and parse it to a get string to accommodate your programming then you might have to specify adding the query string like in your example.

Offline

 

#16 2007-04-18 11:28 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: mod_rewrite loses post data!

Or, as a last resort, check the manual

Note: Query String

The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.

Offline

 

#17 2007-04-18 11:28 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: mod_rewrite loses post data!

But that's beside the point - he's losing post, not get.

Offline

 

#18 2007-04-18 7:17 pm

GreenAlge
The Algae
From: Who knows?
Registered: 2003-09-03
Posts: 802
Website

Re: mod_rewrite loses post data!

So, I managed to fix the problem by using [P], which makes use of mod_proxy and apparently preserves POST data on this server.

Thanks for the help and input, all! smile

_Nik

Offline

 

Board footer

Powered by PunBB 1.2.6
© Copyright 2002–2005 Rickard Andersson