【问题标题】:What is the best way to implement a friendly URL that has multiple variables using mod_rewrite?使用 mod_rewrite 实现具有多个变量的友好 URL 的最佳方法是什么?
【发布时间】:2010-11-21 14:55:24
【问题描述】:

我正在构建一个 web 应用程序,该应用程序是客户端 js 繁重的,数据从服务器以块的形式推送。

我正在尝试为友好的 URL 解决方案实现一个解决方案,该解决方案采用如下 URL:

http://exmample.com/find/SomethingHere-or-there

这相当于一系列不可见的变量。所以它最终可能会这样处理:

http://exmample.com/index.php?loc=London&type=2&priceCat=10-15

我通过将友好 URL 作为参数传递然后返回完整 URL 来获取值。

我的初始实现使用 mod_rewrite 规则转发到提取友好 url 的脚本,在数据库中查询匹配的友好 url 并返回完整的 url 及其映射到的所有参数,用它构建 url 字符串然后转发到 Web 地址,然后它将参数提取到 Web 应用程序前端。

但是,当我使用 php header("Location:http://example.com") 函数时,我会丢失友好的 URL,因此他们会看到带有参数的完整 URL。这是我试图满足的次要要求之一。

然后我认为由于我的网络应用程序是 js 繁重,我应该尝试渲染页面然后将参数从数据库中 ajax 到我的网络应用程序吗?这是最好的方法还是有另一个我不知道的技巧来实现这一点?

没有启用 js 的客户端不是问题。

附上一些代码来概述我的初始实现:

//get the friendly url as a parameter
$goto = explode('=',$_SERVER['QUERY_STRING']);
//$goto[1] = SomethingHere-or-there

//build the friendly URL string
$forwardingURL = 'Location:http://'.$_SERVER['HTTP_HOST'].'/'.getForwardedURL($goto[1]);

//will be http://exmample.com/index.php?loc=London&type=2&priceCat=10-15
header($forwardingURL);

//function that returns the full url param string from the friendly url
function getForwardedURL($friendlyURL){     
$friendlyURL = mysql_escape_string($friendlyURL);
$query = "
SELECT url
FROM FriendlyURLmapping
WHERE friendly_url = \"$friendlyURL\"";

$resultP = mysql_query($query) 

//the full parametised URL will be in $row
$row = mysql_fetch_array($resultP, MYSQL_ASSOC);    
return $row["url"];
}   

【问题讨论】:

    标签: mod-rewrite friendly-url


    【解决方案1】:

    通常的做法是将带有值的 URL 映射到参数化 URL。例如:

    http://example.com/London/2/10-15

    http://example.com/index.php?loc=London&type=2&priceCat=10-15

    这可以在 .htaccess 中像这样完成:

    RewriteEngine on
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /index.php?loc=$1&type=$2&priceCat=$3 [L]
    

    如果可能的话,我会避免重定向。如果您希望完全不同的 URL 映射到参数,例如您的示例(/something-here/index.php?...),那么您需要做的就是重新设计您的应用程序,以便将参数传递给显示页面的函数,或者设置变量并包含另一个执行处理的 PHP 文件。

    【讨论】:

    • 你也可以只使用 mod-rewrite 将域名后面的所有内容导出到 $_URL,然后你可以用斜杠将其分解并用 PHP 解析每个部分。
    • 我同意,流行的做法是在 URL 中构建参数。然而,要求是建立一个非直接参数映射。原因是参数的数量可能会改变,否则我可以假设 URL 映射从:example.com/London/2/10-15 到 example.com/index.php?loc=London&type=2&priceCat=10-15跨度>
    • @Joe:那么,您将参数映射存储在哪里?我回答的后半部分应该会有所帮助,您只需将/something-here 设置为映射到index.php?param=something-here,然后使用该PHP 脚本来显示必要的信息。
    • @DisgruntledGoat 在数据库中。当我将友好转发到我的页面时,我实际上使用友好 url 作为完整参数列表/查询字符串的查找,以传递给客户端进行处理。看看我是否可以修剪一些代码来概述我的实现
    【解决方案2】:

    为什么要重定向到那个参数化的 URL?为什么不直接使用参数化 URL 并返回实际内容?

    所以不要做重定向,做这样的事情:

    $url = 'http://example.com/index.php?loc=London&type=2&priceCat=10-15'; // resolved from http://example.com/find/SomethingHere-or-there
    // split URL into its parts
    $url = parse_url($url);
    // check if requested file exists
    if (is_file($_SERVER['DOCUMENT_ROOT'].$url['path'])) {
        // parse and merge URL parameters
        parse_str($url['query'], $params);
        $_GET = array_merge($_GET, $params);
        // include resolved file
        include $_SERVER['DOCUMENT_ROOT'].$url['path'];
    } else {
        hedaer($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
    }
    

    【讨论】:

    • 目标是获得漂亮的网址。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多