Seems like POST values are lost when .htaccess RewriteRule used. GET values are OK. How to fix?

Several days ago I had a question about removing index.php from the address bar, so the address of the page looks shorter and better. The shortest solution of this problem was (RewriteRule ^index.php / [L,R=301] in the .htaccess file). And it works!

Since I put that string into the .htaccess, some pages are redirected to the main page. I spent a lot of time to guess, why. As I understand, the answer is: with RewriteRule ^index.php / [L,R=301], $_POST parameters are not sent to the next page. $_GET parameters are OK.
Once I remove RewriteRule ^index.php / [L,R=301] from .htaccess, everything becomes fine as usual.
Why does it happen and how to fix that?

Thank you.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The [R] flag will incur a redirect. And user-agents issue a redirect as GET request. There is nothing that can be done if you really want to shorten URLs down to the / root path.

You could however block POST requests specifically from being rewritten/redirected:

RewriteRule ^index.php / [L,R=301]

Solution 2

You could try using [L,R=307] instead. 307’s must not change the request-method according to the spec, but I don’t know how browser implemented 307.

But the root of the problem is the use of <form action="____/index.php" ...

Just leave the action empty to POST to the current url e.g.

Solution 3

I’m using something like:

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(css|images|js)/

# don't rewrite existing files, directories and links

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

# rewrite everything else to index.php

RewriteRule .* index.php [L]


And its working for all requests, rewriting it via index.php file.
If you need to redirect 301 (which stands for Moved Permanently code) check out this question: Is it possible to redirect post data?

Solution 4

POST values will NEVER survive an external redirect (the R=301), it’s a security liability, so browsers will never support that. Remove the R=301 and you will be fine. You just should alter all existing links to the page to the shorter/prettier one (<a>‘s but also form actions etc.)

Solution 5

I had the same problems but my htacces was like this:

RewriteEngine on
RewriteRule .* index.php [NC]

Just change NC for L and everything works fine.

Final code:

RewriteEngine on
RewriteRule .* index.php [L]

Solution 6

In My case I used .htaccess.
Refer : PHP $_POST not working?

action=”booking.php” to action=”booking” worked for me

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply