【问题标题】:htaccess rewrite rules causes error while submitting formhtaccess 重写规则在提交表单时导致错误
【发布时间】:2013-02-08 12:50:22
【问题描述】:

我使用我的 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/languagefolder/...

现在,当用户拨打www.domain.com 时,他将被重定向到languagesubfolder。当beeing被重定向到这个子文件夹的索引页面时,第一件事应该是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.

【问题讨论】:

  • 在提交之前表单中的实际操作是什么?例如,显示源代码并查看它的内容?
  • 请用一些传入和替换 URL 示例更新您的问题。否则很难猜测这些规则的用途和理解您想要实现的目标。规则有一个 regex,它应该与传入 URL 中的 pattern 匹配,但如果没有 URL,则无法确定该模式。你的规则并没有真正的帮助,因为它们不起作用。
  • 你好,我更新了我的问题。谢谢。
  • 旁注:不要使用PHP_SELFit is vulnerable to XSS attacks。相反,要将表单指向当前页面,请使用 action=""
  • 你好,感谢这个有用的提示。

标签: php forms apache .htaccess url-rewriting


【解决方案1】:

使用:

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。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 您好,感谢您帮助我。此代码导致错误 500。尝试这种方式时站点不可用。好的,澄清一下:是的,我确实喜欢干净的网址。所有的结尾都应该被删除。即使有人尝试输入 page.php 或 page.html,他也会被重定向到其原始文件,该文件可以是 php 或 html 文件。下一种情况是我确实使用语言的子文件夹,并且每个子文件夹都有自己的 index.php - 在这种情况下,用户不应该在 url 的末尾看到 index.php,只是像 domain.com/us/ 这样的路径而不是 domain.com/us/index.php 甚至 .../us/index
    • 嗯,您为返回 500 做了什么 URL 请求?关于您的目录和 index.php 文件,第一个重写规则应该处理所有这些。我只是简化为一条规则。如果您还可以在此处注释掉一些行以确定有问题的行,那将有所帮助,因为这对我来说并不明显。
    • 你好,我只是调用 www.domain.com/subfolder/index.php 来得到这个错误。抱歉,我无法确定任何其他行,因为 htaccess 中没有更多行了。谢谢
    【解决方案3】:

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

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

    这解决了你的问题吗?

    开心!

    【讨论】:

    • 您好,感谢您的回答。我发现这行: RewriteRule ^index\.(htm|html|php) domain.com [R=301,L] RewriteRule ^(.*)/index\.(htm|html|php) domain.com/ $1 [R=301,L] 将导致我得到的错误。似乎当我调用 /subfolder/index 时它不会发布数据,可能是因为重写条件为空。
    【解决方案4】:

    您的问题似乎并非来自您的.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 的子文件夹?

    【讨论】:

    • 您好,感谢您的回答。我发现这行: RewriteRule ^index\.(htm|html|php) domain.com [R=301,L] RewriteRule ^(.*)/index\.(htm|html|php) domain.com/$1 [ R=301,L] 将导致我得到的错误。似乎当我调用 /subfolder/index 时它不会发布数据,可能是因为重写条件为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多