简体   繁体   English

如何使RewriteCond更改THE_REQUEST而不更改REQUEST_METHOD

[英]How to make RewriteCond change THE_REQUEST but not REQUEST_METHOD

I got this rewrite rule from the internet: 我从互联网上得到了这个重写规则:

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

The rule works well, but now when I try to send a POST message, this rule will rewrite also the method to GET. 该规则很好用,但是现在当我尝试发送POST消息时,该规则还将方法也重写为GET。 This is my form: 这是我的表格:

<form action="check.php" method="post">
<input type="text" name="email" id="email"/>
<input type='submit' value='check'>
</form>

This is what I got from the server (var_dump($_SERVER)) 这是我从服务器(var_dump($ _ SERVER))获得的

["REQUEST_METHOD"]=>
string(3) "GET"

I am not really familiar with rewrite rules. 我对重写规则不是很熟悉。 Could you tell me how to fix it so that it still process php file extensions but wont touch the REQUEST_METHOD part (from POST to GET)? 您能告诉我如何解决它,以便它仍然可以处理php文件扩展名,但不会碰到REQUEST_METHOD部分(从POST到GET)吗?

Thank you. 谢谢。

UPDATE FULL RULE: 更新完整规则:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /


# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [L,NC] #[R,L,NC]


## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

This is how your .htaccess should look like: 这是您的.htaccess应如下所示的样子:

Options +FollowSymLinks -MultiViews 

RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

You should never use comments on the same lines of the conditions and rules on the .htaccess like this: 切勿在.htaccess的条件和规则的相同行中使用注释,如下所示:

RewriteRule ^ %1 [L,NC] #[R,L,NC]

As it will break the .htaccess from working, comments should be always on a new line. 因为它将中断.htaccess的工作,所以注释应始终放在新的一行。

This is how your form should look like: 这是您的表单应为的样子:

<form action="http://domain.com/check" method="post">
<input type="text" name="email" id="email"/>
<input type="submit" value="check">
</form>

This should properly return you with the value you want as a POST method. 这应该正确地将您想要的值作为POST方法返回给您。

array(1) { ["email"]=> string(4) "text you typed on the input box" } 

You are doing a redirect [R] - so you are posting to a location which is just dumping the POSTed data and redirecting the request (HTTP GET). 您正在执行重定向[R] -因此您正在发布到仅转储已发布数据并重定向请求(HTTP GET)的位置。 The post should be pointed to /dir/foo which could be passed to /dir/foo.php, but you're current .htaccess doesn't allow for that. 该帖子应指向/ dir / foo,可以将其传递给/dir/foo.php,但您目前是.htaccess不允许这样做的对象。

Your form should look something like: 您的表格应类似于:

<form action="/check" method="post">
      <input type="text" name="email" id="email"/>
      <input type='submit' value='check'>
</form>

Here's an example of internal routing to .php files. 这是内部路由到.php文件的示例。

 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f  # As long as it's not a file
 RewriteCond %{REQUEST_FILENAME} !-d  # As long as it's not a directory
 RewriteRule (.*) $1.php [L,QSA]

Hope that points you in the right direction. 希望能为您指明正确的方向。

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

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