使用mod_rewrite实现具有多个变量的友好URL的最佳方法是什么? [英] What is the best way to implement a friendly URL that has multiple variables using mod_rewrite?

查看:57
本文介绍了使用mod_rewrite实现具有多个变量的友好URL的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个客户端js繁重的Web应用程序,其中数据被从服务器中逐块推送.

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

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

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

我通过传递友好的URL作为参数来获取值,然后获取完整的URL.

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

我的初始实现使用mod_rewrite规则转发到一个脚本,该脚本提取出友好的url,向数据库查询匹配的友好的url,并返回包含其映射到的所有参数的完整url,并以此来构建url字符串,然后进行转发到网址,然后将参数提取到Web应用程序的前端.

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.

但是,当我使用php标头时,我丢失了友好的网址(位置: http://example.com )函数,这样他们就可以看到带有参数的完整URL.这是我要满足的次要条件之一.

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.

然后我认为,由于我的Web应用程序的js繁重,我是否应该尝试呈现页面,然后将参数从db ajax到我的Web应用程序?这是最好的方法还是实现我不知道的另一种技巧?

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?

未在客户端启用js并不是问题.

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

推荐答案

通常的做法是将URL中包含值的URL映射到参数化URL.例如:

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

http://example.com/伦敦/2/10-15

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

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

这可以在.htaccess中完成:

This can be done like so in .htaccess:

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

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

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.

这篇关于使用mod_rewrite实现具有多个变量的友好URL的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆