1

After turning URL rewriting on and I've encountered one problem around Javascript HTTP requests.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} \ /(.+)\.php(\?|\ |$)
RewriteRule ^ /%1 [L,R=301]

xmlhttp.open("POST", "php/msg_send.php", true); POST method simply didn't work but xmlhttp.open("GET", "php/language_check.php?lang="+langSelect, true);GET method continued working well as before.

RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]

After adding this code in my .htaccess file POST method started working well.

I'm little confused because GET method worked fine after turning url rewriting on, but POST method did not. Is it true that xmlhttp GET can work well without adding some lines in .htaccess file but POST method can't? I would like someone to explain why is GET method working after url rewriting (deleting .php extension) but POST method doesn't.

Thanks in advance.

lbartolic
  • 873
  • 1
  • 10
  • 23

2 Answers2

1

The reason is because you are redirecting R=301 and when you redirect a POST request, the request body, where the POST data resides, isn't guaranteed to be sent along with the redirect. If you are sending the POST using javascript, and the browser's URL location bar doesn't change, then you don't need to rewrite any POST requests at all (since I'm guessing the goal of your rules is to remove the "php" extension from your URLs).

You can clean up your rules by adding a few more things:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \ /(.+)\.php(\?|\ |$)
RewriteRule ^ /%1 [L,R=301]
Jon Lin
  • 135,941
  • 26
  • 200
  • 209
  • 1
    @kizzwiz in a GET request, data is part of the actual URL in the form of a query string (e.g. the `?param=value&param2=value2` stuff), but in a POST request, those parameters are encoded as part of the body of the request (not seen in the URL). See: http://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request – Jon Lin Sep 24 '14 at 15:11
1

Here is what happens to a POST request:

  1. Browser sends a POST request to server to URL: http://domain.com/php/msg_send.php
  2. Server does a 301 redirect to http://domain.com/php/msg_send
  3. POST data is discarded on redirect as client will perform a GET request to the URL http://domain.com/php/msg_send received by the 301.
anubhava
  • 664,788
  • 59
  • 469
  • 547