我最近做了一些研究,以了解如何修改我的.htaccess文件以隐藏URL中的.php扩展名。我通过以下代码了解了我的工作方式:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.guitrum.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.guitrum.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.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页面上,我在页面中有一个加密类作为包含文件,代码如下:
<?php
//source: http://stackoverflow.com/questions/2448256/php-mcrypt-encrypting-decrypting-file
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: http://stackoverflow.com/questions/2448256/php-mcrypt-encrypting-decrypting-file
?>
我在页面上做的第一件事是设置一个新的变量,其密码加密如下:
<?php
include ("includefiles/EncryptionUtilities.php");
$passworde = Encryption::encrypt($_POST['password']);
setcookie('password', $passworde, time() + (60 * 5));
?>
通常它会正常工作,现在它会抛出错误说:
将空字符串传递给EncryptionUtilities.php,并且无法修改标头信息 - 已发送的标头。
我认为.htaccess文件存在问题,不允许POST方法在页面之间进行通信。
答案 0 :(得分:3)
使用外部重定向POST数据不会被重定向并丢失。用此替换.php
规则:
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]