【问题标题】:Multiple query parameter RESTful URL with .htaccess带有 .htaccess 的多个查询参数 RESTful URL
【发布时间】:2010-08-13 09:02:46
【问题描述】:

我想为我正在开发的网站使用以下 RESTful URL。

http://mysite.com/Products/category=bags&colours=black

谁能告诉我如何使用 .htaccess 实现这一目标?

奥斯卡

【问题讨论】:

    标签: .htaccess restful-url


    【解决方案1】:

    这是一个 .htaccess,用于捕获除文件或目录冲突之外的任何内容并路由到 /index.php:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    我们可以使用 /index.php 作为我们的请求路由器。在 PHP 中,我们只是简单地得到这样的请求:

    <?php
    $url_path = $_SERVER['REQUEST_URI'];
    // Parse your URL path here...
    

    也就是说,您的 URL 模式是非标准的,我强烈建议您不要这样做,因为代理和浏览器将对 & 和等号进行 URL 编码。让我建议以下其中一项(也可以使用小写,因为它有助于验证 URL):

    http://example.com/products/categories/bags/colours/black
    http://example.com/products/category_bags/colours_black
    http://example.com/products?category=bags&colours=black
    http://example.com/products/categories/bags?colours=black
    http://example.com/products/bags?colours=black
    

    要查看将在 URL 路径中编码的内容,只需谷歌它并查看谷歌的搜索 URL。

    希望这会有所帮助。

    -迈克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 2018-06-07
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      相关资源
      最近更新 更多