4

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

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

我有两个文件将包含相同的代码(复制和重命名)。如果我调用page1.phpurl 将显示为page1.php. 一切正常,而我将尝试登录或导致一些错误。现在,当我将调用index.php(具有相同的代码!) url 中的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以及.../page.htmlor .../page.htm

4

4 回答 4

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,您可以通过查看 net 面板并在提交表单之前单击 persist 来实时看到这种情况。您将看到 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。

于 2013-03-06T15:17:21.170 回答
0

从查看 .htaccess 并阅读您的描述来看,您想要做什么似乎有点不清楚。听起来您想要干净的 URL(即从不显示文件扩展名),但您却谈论发布到 index.php 等。如果你想使用干净的 URL,你需要在整个过程中使用干净的 URL(即 post to/和 not /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]
于 2013-02-22T20:04:30.707 回答
0

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

.ht 访问:

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]

索引.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

于 2013-03-03T15:10:01.017 回答
0

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

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

这解决了你的问题吗?

开心!

于 2013-03-04T00:21:50.600 回答