简体   繁体   English

404通过.htaccess重定向时如何获取`$ _POST`查询?

[英]How to get `$_POST` query when 404 redirected through .htaccess?

This question is somewhat related to my previous question . 这个问题与我之前的问题有些相关。 The trick of using $_SERVER['REDIRECT_QUERY_STRING'] seems to work only for $_GET variables. 使用$_SERVER['REDIRECT_QUERY_STRING']的技巧似乎只适用于$_GET变量。

Well, I've an index.php file which handles all the 404 redirects . 好吧,我有一个index.php文件来处理所有404 redirects

If a user requests for a page which doesnt exist, say, apple.php?item=23 , then using $_SERVER['REDIRECT_QUERY_STRING'] I can get the $_GET variable item=23 , but if the variable is not $_GET but $_POST then $_SERVER['REDIRECT_QUERY_STRING'] doesn't work. 如果用户请求一个不存在的页面,比如apple.php?item=23 ,那么使用$_SERVER['REDIRECT_QUERY_STRING']我可以得到$_GET变量item=23 ,但是如果变量不是$_GET但是$_POST然后$_SERVER['REDIRECT_QUERY_STRING']不起作用。

How can I get $_POST variable when I redirect it to index.php using the following .htaccess setting 当我使用以下.htaccess设置将其重定向到index.php时,如何获取$_POST变量

ErrorDocument 404 /index.php ErrorDocument 404 /index.php

Check answer here : http://www.brainonfire.net/blog/apache-pitfall-errordocument-post/ 在这里查看答案: http//www.brainonfire.net/blog/apache-pitfall-errordocument-post/

I solved my problem using this. 我用这个解决了我的问题。


OR put this in your .htaccess file : 或者把它放在.htaccess文件中:

RewriteEngine On
    RewriteBase /


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /yourErrorDocument.php [L]

With the following directive: 使用以下指令:

ErrorDocument 404 /index.php

the apache webserver does an internal redirect to the new location. apache webserver执行内部重定向到新位置。 Internal means, the client (browser) won't change the URL in it's address bar because that redirect is not communicated to the browser. 内部意味着,客户端(浏览器)不会更改其地址栏中的URL,因为该重定向不会传达给浏览器。

Because it's an redirect, the POST request is turned into a GET request. 因为它是重定向,所以POST请求变为GET请求。

You can see this by looking into the following two $_SERVER variables: 您可以通过查看以下两个$_SERVER变量来看到这一点:

$_SERVER['REDIRECT_REQUEST_METHOD'] # POST
$_SERVER['REQUEST_METHOD'] # GET

So in short, you can not use the ErrorDocument directive to do URL rewriting for HTTP POST requests. 简而言之,您不能使用ErrorDocument指令对HTTP POST请求进行URL重写。

You need to use the mod_rewrite module for this or create your own apache handler. 您需要使用mod_rewrite模块或创建自己的apache处理程序。

Use the FallbackResource directive instead of the ErrorDocument directive of Apache: it does the trick FallbackResource on Apache website 使用FallbackResource指令而不是Apache的ErrorDocument指令:它在Apache网站上使用FallbackResource技巧

Example: 例:

FallbackResource /404.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM