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.

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

1 Comment

@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: stackoverflow.com/questions/14551194/…
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.

2 Comments

thanks, but why is then GET method working even server does a 301 redirect from php/language_check.php?lang= to php/language_check/?lang=?
GET request works since all the parameters are part of URL itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.