更改我的 .htaccess 文件后,我的 php 上的 POST 方法不再有效

After changing my .htaccess file, my POST method on my php no longer works

我最近做了一些研究,以了解如何修改我的 .htaccess 文件以隐藏 URL 中的 .php 扩展名。我使用以下代码让它按照我想要的方式工作:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.guitrum.com/ [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.guitrum.com/ [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ .php [L]

ErrorDocument 404 /404.php

DirectoryIndex index.php

不幸的是,我的 php 脚本的一部分现在损坏了。请记住,在修改我的 .htaccess 文件之前,一切正常。在登录页面上,我有一些脚本可以使用 POST 方法传递一些用户输入,如下所示:

<form method='POST' action='loginconfirm.php'>
Password: <input type='password' name='password'></input>
<input type='submit' name='submit' value='Go'></input>
</form>

在 loginconfirm.php 页面上,我有一个加密 class 作为页面中包含的文件,代码如下:

<?php
//source: 
class Encryption {

    const CYPHER = MCRYPT_RIJNDAEL_256;
    const MODE = MCRYPT_MODE_CBC;
    const KEY = 'SecretKey';

    public function encrypt($plaintext) {
        $td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, self::KEY, $iv);
        $crypttext = mcrypt_generic($td, $plaintext);
        mcrypt_generic_deinit($td);
        return rawurlencode(base64_encode($iv . $crypttext));
    }

    public function decrypt($crypttext) {
        $crypttext = rawurldecode($crypttext);
        $crypttext = base64_decode($crypttext);
        $plaintext = '';
        $td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
        $ivsize = mcrypt_enc_get_iv_size($td);
        $iv = substr($crypttext, 0, $ivsize);
        $crypttext = substr($crypttext, $ivsize);
        if ($iv) {
            mcrypt_generic_init($td, self::KEY, $iv);
            $plaintext = mdecrypt_generic($td, $crypttext);
        }
        return trim($plaintext);
    }

}

//source: 
?>

我在页面上做的第一件事是设置一个新变量,密码加密如下:

<?php
include ("includefiles/EncryptionUtilities.php");
$passworde = Encryption::encrypt($_POST['password']);
setcookie('password', $passworde, time() + (60 * 5));
?>

通常情况下它可以正常工作,现在它抛出错误说:

an empty string was passed into EncryptionUtilities.php, and cannot modify header information - headers already sent.

我认为 .htaccess 文件有问题,不允许 POST 方法在页面之间通信。

使用外部重定向 POST 数据不会被重定向并且会丢失。将您的 .php 规则替换为:

# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ / [R=301,L,NE]