3

To maintain consistency across my app, I need all requests to include www so I use .htaccess file like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,NC,L]
</IfModule>

It works just fine. HOWEVER, when POST requests are sent without the www the form data gets stripped out. Is this expected? Is there a way to correct this?

emersonthis
  • 32,822
  • 59
  • 210
  • 375

1 Answers1

3

A Redirect response with status such as 301,302, or 303 is always handled as a GET in every browser I've encountered. Hence, a POST request redirected will be seen by the client browser and it will issue a GET request to the URL provided in the redirect response. See my answer to the following question and the comments others added for details how you might work around this gracefully: Apache 301 Redirect and preserving post data

Community
  • 1
  • 1
Ray
  • 40,256
  • 21
  • 101
  • 138
  • Thanks Ray. My issue is not relate to browser(s) because I'm making an API request programmatically. Does what you said about 301 redirects apply to any agent? Most of my testing is being done using the Chrome Postman plugin, but in production it will come from a java app running on a server. – emersonthis Jan 16 '14 at 22:27
  • I just read your other post and it sounds like the 301 converts everyone to a GET request. Browser or no browser. – emersonthis Jan 16 '14 at 22:37
  • @SDP It totally depends on the client agent, but browsers automatically redirects as described. Some clients and libraries (like curl, wget, etc...) allow you not follow redirects automatically. The java rest library you're using probably can be configured to not auto redirect when making a post, and when you hit a redirect construct a new post to the new URL (assuming the resource accepts post requests). Remember: the actual redirect handling happens at the client. All the server does is return a URL and 30x status code. – Ray Jan 16 '14 at 22:37