使用 mod_rewrite 实现具有多个变量的友好 URL 的最佳方法是什么?

发布于 2024-08-02 02:31:36 字数 1827 浏览 6 评论 0原文

我正在构建一个 Web 应用程序,该应用程序的客户端 js 较多,数据从服务器分块推送。

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

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

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

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

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

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

但是,当我使用 php header("Location:http://example.com") 函数时,我丢失了友好的网址所以他们会看到带有参数的完整 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"];
}   

I am building a web application that is client side js heavy with data being pushed in chunks from the server.

I am trying to implement a solution for a friendly URL solution that takes a URL like:

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

That equates to a series of variables not visible. So it might end up processing as this:

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

I get the values by passing the friendly URL as a parameter then getting back the full URL.

My initial implementation used a mod_rewrite rule to forward to a script that extracts the friendly url, query the db for a matching friendly url and return the full url with all the params it maps to, build the url string with this then forward to the web address which it will then extract the params to the web app front side.

However I lose the friendly url when I use the php header("Location:http://example.com") function so they see the full URL with params. This is one of the secondary requirements that i am trying to fulfill.

I then thought that as my web app is js heavy, should I try render the page then ajax the params out of the db to my web app? Is this the best way or is there another trick to achieve this that i don't know about?

Not having js enabled client side is not an issue.

attached is some code to outline my inital implementation:

//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"];
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恬淡成诗 2024-08-09 02:31:36

通常的做法是将其中包含值的 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 文件。

Normal practice is to map URLs with values in them to the parameterized URL. For example:

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

This can be done like so in .htaccess:

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

I would avoid redirecting if at all possible. If you want completely different URLs to map to parameters, like your example (/something-here to /index.php?...) then all you need to do is rework your application so that you either pass the parameters to a function that displays the page, or set the variables and include another PHP file that does the processing.

我不是你的备胎 2024-08-09 02:31:36

为什么要重定向到该参数化 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');
}

Why do you redirect to that parameterized URL? Why don’t you just use that parameterized URL and return the actual contents?

So instead of doing a redirect, do something like this:

$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');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文