【问题标题】:htaccess RewriteRule passes index.php instead of stringhtaccess RewriteRule 传递 index.php 而不是字符串
【发布时间】:2017-12-06 15:58:26
【问题描述】:

所以我正在尝试了解 .htaccess RewriteRules 并面临以下问题:

“/home”应该打开“index.php?page=home”

我在“index.php”中有一个 php $_GET['page'],但它每次都通过“index.php”而不是“home”。

这是文件

.htaccess

RewriteEngine On
RewriteRule ^/?(.+)/?$ index.php?page=$1 [L]

index.php PHP 部分

<?php

        if(isset($_GET['page'])) {
            $path = $_GET['page'] . ".php";
            if (file_exists($path)) {
                include($path);
            }
            else {
                echo "File not found: " . $path;
            }
        }
        else {
            include("home.php");
        }

?>

输出总是:

File not found: index.php.php

我在这两个方面都遇到了这个问题:web-server 和 apache。

有什么建议吗?如有必要,我会用更多信息更新我的问题。

【问题讨论】:

  • 试试这个RewriteRule ^(.*)(/|)$ /index.php?page=$1 [QSA,L](我没有在我们的服务器上测试过,但它和我们的非常相似)
  • 如果你有一组已知的页面,你可以使用类似RewriteRule ^(home|about|catalog)(/|)$ /$1.php [QSA,L]

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

您必须从重写规则中跳过现有文件和目录以避免循环规则:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+?)/?$ index.php?page=$1 [L,QSA]

【讨论】:

  • 有效!你把它修好了。它通过在最后添加“QSA”来工作,并且在添加 RewriteConditions 时它可以工作(没有“QSA”)。谢谢:)
猜你喜欢
  • 1970-01-01
  • 2015-06-13
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 2013-02-25
  • 2019-01-02
  • 1970-01-01
  • 2014-11-26
相关资源
最近更新 更多