Seems Like Post Values Are Lost When .Htaccess Rewriterule Used. Get Values Are Ok. How to Fix

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

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:

RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^index.php / [L,R=301]

.htaccess redirect all POST request with data to a file

You are using the wrong status code. In a nutshell, the 302 code is not requesting that the browser retransmit all data (the POST request); you need to use the 307 code instead.

To do this under Apache, update that last rule to end with

[QSA,L,R=307]

If you are only interested in the POST data, you don't even need the QSA flag.

For the gory details, you can see the entire list of HTTP status codes and their meanings here. The reason the 302 code is not functioning the way you want it to is explicitly called out in on the protocols page as follows (emphasis mine)

Note: RFC 1945 and RFC 2068 specify that the client is not allowed
to change the method on the redirected request. However, most
existing user agent implementations treat 302 as if it were a 303
response, performing a GET on the Location field-value regardless
of the original request method
. The status codes 303 and 307 have
been added for servers that wish to make unambiguously clear which
kind of reaction is expected of the client.

GET Variables are confused when HTACCESS is involved

The problem is that, with your rewrite rules, the first one in the list is:

RewriteRule ^index/r/(.*)$ /index.php?r=$1

Because that matches the / character $_GET['r'] is being assigned the value policy/cookiepolicy.

You can either exclude the / from the match by using ([^/]*) instead of (.*) or simply reverse the order of the rules so that the second match is evaluated before the first:

RewriteRule ^index/r/(.*)/(.*)$ /index.php?r=$1&tab=$2 [L]
RewriteRule ^index/r/(.*)$ /index.php?r=$1 [L]

That way /policy/cookiepolicy is caught by the first match and:

$_GET['r'] = 'policy'
$_GET['tab'] = 'cookiepolicy'

Is it possible to redirect post data?

Try this:

# redirect mail posting to index
RewriteRule send-mail index.php?send-mail [NC,P]

"P" acts like "L" in that it stops processing rules but it also tells the module that the request should be passed off to the proxy module intact (meaning POST data is preserved).

htaccess rewriterule preserve post data

  1. Remove the contact directory.
  2. Put this rule in the htaccess file in your document root

    RewriteRule ^contact/?$ /contact.php [L]

The reason why you are losing your POST data and getting redirected is because if there is a contact directory, and you request /contact, the mod_dir module will redirect you to /contact/ to enforce trailing slashes for requests to directories. The redirect and the rewrite both get applied and thus you see /contact.php.

Losing php://input after a mod_rewrite

If anybody else is having trouble with this or a similar problem, adding the redirect flag, with http status code 307, to my rewrite rule worked for me. I saw the suggestion here.

RewriteRule ^api/(.*)$ /apiHandler.php [R=307]


Related Topics



Leave a reply



Submit