【问题标题】:htaccess and two post parametershtaccess 和两个 post 参数
【发布时间】:2012-07-21 23:07:21
【问题描述】:

我为我的网站开发了一个小型的自定义 MVC 框架。它是 index.php 检查 $_POST['page'] & $_POST['subpage'] 并包含指定参数的相关 php 文件。

例如:index.php?page=foods (包括 view/foods.php)

例如:index.php?page=foods&subpage=pizza (包括 view/foods/pizza.php)

现在我想使用 .htaccess 清理我的 URL。我尝试了几种方法来传递第二个参数。但我失败了。这工作正常...

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

但是当我尝试传递第二个参数时,它不能正常工作。这一项改变 css/js/iamges 路径。

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

RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?page=$1&sub=$2

【问题讨论】:

    标签: php css model-view-controller .htaccess post


    【解决方案1】:

    如果您使用 QSA(查询字符串附加)标志,您可以简单地将整个请求字符串传递给索引文件 -

    QSA - 将原始请求 URL 中的任何查询字符串附加到重写目标中创建的任何查询字符串

    所以你的规则看起来和这个类似 -

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

    在您的索引文件中,您可以通过斜线.explode() -

    $splitArr = explode('/',$_GET['uri']);
    

    和/或使用内置的 parse_url() 函数 - http://www.php.net/manual/en/function.parse-url.php

    parse_url — 解析 URL 并返回其组件

    【讨论】:

    • 我试过这个。但它在 Localhost 上返回错误 500。 htacess 有什么问题吗?
    • 是的。我删除了 htacess cmets 并且工作正常。但我回显 $_GET['uri'],它只返回 index.php。我需要改变什么来传递第二个参数吗?
    • 但这不包括实际存在的文件/目录。我们能做些什么呢? URL 工作正常。一个也是唯一的问题是 CSS、JS 和图像。
    • 这就是应该发生的事情——你希望你所有的 url 到资源,这意味着直接给出图像、样式表和脚本,只有你友好的 url 被重写......
    • 是的。因为当我在 index.php 中使用 include() 时,它会排除每个图像/css/js 等...
    【解决方案2】:

    大多数这种类型的 url-rewriters 明确排除实际存在的文件/目录:

    RewriteRule %{REQUEST_FILENAME} !-d
    RewriteRule %{REQUEST_FILENAME} !-f
    

    【讨论】:

      【解决方案3】:
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f [OR]
      RewriteRule ^public/(js|css|images)(/.*|$) - [L]
      RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
      

      这就是我终于做到了!!!

      【讨论】:

        猜你喜欢
        • 2020-02-13
        • 1970-01-01
        • 2012-02-08
        • 1970-01-01
        • 2012-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多