htaccess重写规则在提交表单时会导致错误

时间:2013-02-22 19:44:41

标签: php forms apache .htaccess url-rewriting

我使用我的htaccess文件来配置我网站上网址的布局。

目前问题是我不能再发送任何表格了。

我有两个完全包含相同代码的文件(复制和重命名)。如果我致电page1.php,则网址将显示为page1.php。一切正常,我会尝试登录或导致一些错误。现在,当我打电话给index.php(具有相同的代码!)时,htaccess会在网址中隐藏index.php!但是当我测试登录脚本时,发布表单时不会发生任何事情。它只是刷新网站。所以我不知道为什么会发生这种情况?

这种行为将由htaccess通过重写规则彻底造成。我删除了htaccess并再次尝试。如果没有它可行,但仍然有这个丑陋的网址。

可以显示htaccess中的内容:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule ^.*\.html$ %1.php [L]  

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

AddType application/x-httpd-php .htm .html

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteRule ^index\.(htm|html|php) http://www.domain.com/ [R=301,L]
## rewrite the index file in subdirectories
RewriteRule ^(.*)/index\.(htm|html|php) http://www.domain.com/$1/ [R=301,L]

,表格将发送:

form action = "<?php echo $_SERVER['PHP_SELF']?>" method= "post"...

所以,如果有人可以帮助我,我真的很感激。

更新

提供我想要实现的更多信息:

首先,结构如下:由于语言不同,根结构如下:

www.domain.com/languagefolder/...

现在,当用户呼叫www.domain.com时,他将被重定向到languagesubfolder。当重定向到此子文件夹的索引页面时,首先应该是www.domain.com/languagesfolder/index.php不可见,因此它只是www.domain.com/languagesfolder/。这将自动完成。没关系。但是,在调用www.domain.com/languagesfolder/index.php时,应将其重新设为www.domain.com/languagesfolder/,以便用户无法看到.../index.php。这似乎更专业。

接下来的事情是,在其他页面上将提供链接,以便用户只需输入.../page而不是.../page.php,这样就不会显示文件扩展名。最后但并非最不重要的是,最后一件事是当用户试图找出它是什么类型的文件时,我想通过重写扩展来阻止SQL注入,以便用户可以键入.../page.php以及{{ 1}}或.../page.html

4 个答案:

答案 0 :(得分:4)

使用:

form action = "<?php echo $_SERVER['PHP_SELF']?>" method= "post"...

发布表单可能会产生以下内容:

form action = "/index.php" method = "post"...
你的代码中的

。您遇到问题的原因是:

RewriteRule ^index\.(htm|html|php) http://www.domain.com/ [R=301,L]

它是否正在捕捉您发布到的index.php,然后将您重定向到http://www.domain.com/ 而不您的帖子数据。

如果您使用firefox并安装了firebug,您可以通过查看网络面板并在提交表单前单击“保持”来实时查看此情况。您将看到POST /index.php的301重定向,然后是http://www.domain.com/的GET。

您可以通过替换以下行来解决此问题:

form action = "<?php echo $_SERVER['PHP_SELF']?>" method= "post"...

使用:

form action = "/" method= "post"...

或:

form action = "<?php echo rtrim($_SERVER['PHP_SELF'], 'index.php'); ?>" method= "post"...

这与使用str_replace相同,但只会删除最右侧出现的“index.php”。

HTH。

答案 1 :(得分:0)

从查看.htaccess并阅读您的描述看起来有点不清楚您要做什么。听起来你想要干净的URL(即从不显示文件扩展名),但你谈到发布到index.php等。如果你想使用干净的URL,你需要使用干净的URL(即发布到/而不是/index.php)。

您应该从.htaccess中删除AddType声明,因为您永远不会请求扩展名为.htm或.html的文件,因为所有请求都是通过干净的URL进行的。您可能不需要在*.index请求上执行任何重定向,除了作为遗留用例(对于您网站的外部链接)。

您应该能够将.htaccess简化为以下内容:

RewriteEngineOn
# redirect legacy index links. Do this first to clean up URL. No need to use full URL here unless directly to different domain
RewriteRule ^(.*)index\.(html|htm|php)$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]

答案 2 :(得分:0)

您的问题似乎不是来自.htaccess。这是我试过的。

htaccess的:

RewriteEngine On
RewriteBase /stackoverflow/15032042

RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule ^.*\.html$ %1.php [L]  

# I don't understand this RewriteRule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

AddType application/x-httpd-php .htm .html

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteRule ^index\.(htm|html|php)$ http://www.workspace.domain.tld/stackoverflow/15032042/ [R=301,L]
RewriteRule ^(.*)/index\.(htm|html|php) http://www.workspace.domain.tld/stackoverflow/15032042/$1/ [R=301,L]

的index.php:

if(isset( $_POST))
    var_dump($_POST);
echo '<form action="" method="POST"><input type="hidden" name="test" value="1" /><input type="submit" /></form>';

然后当我点击http://www.workspace.domain.tld/stackoverflow/15032042/并提交表单时,$_POST会正确显示。

或许你没有想到你的子文件夹RewriteBase

答案 3 :(得分:0)

要转发查询字符串,您还需要将QSA RewriteRule flag添加到规则中,否则将丢弃查询字符串。在大多数情况下,这也是一种很好的做法,有助于保持理智,使用L标志,这将阻止在规则满足后进一步处理规则。你真的不想同时搞乱多个规则,而是制定单独的301规则并在返回时处理重定向。

RewriteRule ^(.*)$ $1.php [L,QSA]

这能解决你的问题吗?

的nJoy!