Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php htaccess重写规则在提交表单时导致错误_Php_Forms_Apache_.htaccess_Url Rewriting - Fatal编程技术网

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

Php htaccess重写规则在提交表单时导致错误,php,forms,apache,.htaccess,url-rewriting,Php,Forms,Apache,.htaccess,Url Rewriting,我使用htaccess文件在我的站点上配置URL的布局 目前的问题是,我不能再发送任何表格了 我有两个文件,完全将包含相同的代码(复制和重命名)。如果我调用page1.php,url将显示为page1.php。一切正常,而我将尝试登录或导致一些错误。现在,当我调用index.php(代码相同!)时,htaccess会像它应该做的那样在url中隐藏index.php!但是当我测试登录脚本时,在发布表单时不会发生任何事情。它只是刷新站点。所以我不知道为什么会这样 此行为将明确由htaccess通过重

我使用htaccess文件在我的站点上配置URL的布局

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

我有两个文件,完全将包含相同的代码(复制和重命名)。如果我调用
page1.php
,url将显示为
page1.php
。一切正常,而我将尝试登录或导致一些错误。现在,当我调用
index.php
(代码相同!)时,
htaccess
会像它应该做的那样在url中隐藏
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
时,他将被重定向到
语言子文件夹
。当Being重定向到此子文件夹的索引页时,第一件事应该是
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
以及
../page.html
../page.htm

查看.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]

您的问题似乎不是来自您的
.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>';
if(isset($\u POST))
变量转储($\u POST);
回声';
然后当我点击
http://www.workspace.domain.tld/stackoverflow/15032042/
并提交表单,
$\u POST
显示正确


或者您可能没有想到您的子文件夹中有
RewriteBase

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

RewriteRule ^(.*)$ $1.php [L,QSA]
这解决了你的问题吗

nJoy!

使用:

form action = "<?php echo $_SERVER['PHP_SELF']?>" method= "post"...
在代码中。该行出现问题的原因:

RewriteRule ^index\.(htm|html|php) http://www.domain.com/ [R=301,L]
form action = "<?php echo $_SERVER['PHP_SELF']?>" method= "post"...
它捕获您发布到的index.php,然后在没有发布数据的情况下将您重定向到

如果您使用firefox并安装了firebug,您可以通过查看网络面板并在提交表单之前单击“持久化”实时看到这种情况。您将看到POST/index.php的301重定向,然后是GET for

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

RewriteRule ^index\.(htm|html|php) http://www.domain.com/ [R=301,L]
form action = "<?php echo $_SERVER['PHP_SELF']?>" method= "post"...
或:

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


HTH.

在提交表单之前,表单中的实际操作是什么?例如,显示源代码并查看其内容?请使用一些传入和替换URL示例更新您的问题。否则很难猜测这些规则是什么,也很难理解您想要实现什么。这些规则有一个
regex
,应该与匹配传入URL中的ode>模式,但是没有URL就无法确定该模式。您的规则不起作用,因此没有真正的帮助。您好,我更新了我的问题。谢谢。旁注:不要使用
PHP\u SELF
。相反,要将表单指向当前页面,请使用
action=“”
hello,感谢您提供的有用提示。hello,感谢您的帮助。此代码导致错误500。尝试这种方式时,网站不可用。好的,澄清一下:是的,我确实希望有干净的URL。所有结尾都应该删除。即使有人试图输入page.php或page.html,他也会被重定向到其原始文件,该文件可能是php或html文件。下一种情况是,我会使用语言的子文件夹,每个子文件夹都有自己的index.php-在这种情况下,用户不应该在url的末尾看到index.php,只是像domain.com/us/这样的路径,而不是domain.com/us/index.php,甚至…/us/indexHmm。您对返回500的url请求是什么?关于您r目录和index.php文件中的第一条重写规则
form action = "/" method= "post"...
form action = "<?php echo rtrim($_SERVER['PHP_SELF'], 'index.php'); ?>" method= "post"...