PHP + HTACCESS + mod_rewrite +不同长度的URL段 [英] PHP + HTACCESS + mod_rewrite + different length url segments
问题描述
对,大家下午好(嗯,今天是英国的下午!)
Right, Good afternoon all (well, it is afternoon here in the UK!)
我正在编写一个使用友好URL的(PHP/MySQL)网站.
I am in the process of writing a (PHP/MySQL) site that uses friendly URLs.
我已经设置了htaccess(启用了mod_rewrite),并且具有可以处理"/"的基本脚本,然后其他所有内容都在?"之后进行处理.在同一脚本中. IE.我能够确定用户是否尝试放置example.com/about
,example.com/about/the-team
或example.com/join/?hash=abc123etc
等.
I have set up my htaccess (mod_rewrite enabled) and have a basic script that can handle "/" and then everything else is handled after "?" in the same script. I.e. I am able to work out whether a user has tried to put example.com/about
, example.com/about/the-team
or example.com/join/?hash=abc123etc
etc.
我的问题是如何处理可变长度的URL,例如(示例):
My question is how do I handle variable length URLs such as (examples):
example.com/about
(仅节点)
example.com/about/the-team
(节点+ seo-页面标题)
example.com/projects
(仅节点)
example.com/projects/project-x
(节点+子节点)
example.com/projects/project-x/specification
(节点+子节点+ SEO友好标题)
example.com/news/article/new-article
(节点+子节点+ SEO友好标题)
example.com/join/?hash=abc123etc&this=that
(节点+查询对)
example.com/about
(node only)
example.com/about/the-team
(node + seo-page-title)
example.com/projects
(node only)
example.com/projects/project-x
(node + sub-node)
example.com/projects/project-x/specification
(node + sub-node + seo-friendly-title)
example.com/news/article/new-article
(node + sub-node + seo-friendly-title)
example.com/join/?hash=abc123etc&this=that
(node + query pair)
但是,节点"(第一个参数),子节点"(第二个参数)或"seo友好页面标题"可能丢失或未知(数据库控制),因此我无法将处理专门放在.htaccess中.记住:我(我认为!)已经有了一个有效的htaccess,可以将所有内容正确转发到我的PHP处理脚本中.找不到的所有内容都将转发到CMS"404".
BUT, the "nodes" (first argument), "sub-nodes" (second argument) or "seo-friendly page titles" may be missing or unknown (database controlled) so I cannot put the processing in .htaccess specifically. Remember: I have already (I think!) got a working htaccess to forwards everything correctly to my PHP processing script. Everything not found will be forwarded to a CMS "404".
我认为我的客户最多可以有3个参数(然后其他所有参数都在?"之后).
I think my client will have a maximum of THREE arguments (and then everything else will be after "?").
有人尝试过这种方法吗?或者有地方从数据库结构入手,或者如何处理我是否提出了上述任何可能性?
Has anyone tried this or have a place to start with a database structure or how to handle whether I have put any of the above possibilities?
我曾在先前的项目中尝试过,但始终不得不编写CMS来迫使用户(至少添加页面)至少拥有一个节点或一个节点+子节点+ seo-title-title,这是我想要的摆脱...
I have tried in a previous project but have always had to resort to writing the CMS to force the user to have (whilst adding pages) at least a node OR a node + subnode + seo-friendly-title which I would like to get away from...
我不希望通过尝试找到参数的每种可能性直到找到匹配项而对数据库搜索造成太大压力的脚本...或者这是我要实现自己的目标的唯一方法在问吗?
I don't want a script that will put too much strain on database searches by trying to find every single possibility of the arguments until a match is found... or is this the only way if I want to implement what I'm asking?
非常感谢!
推荐答案
您可以像这样满足不同数量的匹配项:
You can cater for different numbers of matches like this:
RewriteRule ^/([^/])* /content.php?part1=$1 [L,QSA,NC]
RewriteRule ^/([^/])*/([^/])* /content.php?part1=$1&part2=$2 [L,QSA,NC]
RewriteRule ^/([^/])*/([^/])/([^/])* /content.php?part1=$1&part2=$2&part3=$3 [L,QSA,NC]
在[^/]匹配'/'以外的任何字符的位置,然后由于该术语包含在()括号中,因此可以在重写的URL中使用它.
Where [ ^ / ] to matches any character other than '/' - and then because that term was enclosed in () brackets, it can be used in the re-written URL.
QSA将处理所有参数,并将它们正确地附加到重写的URL.
QSA would handle all the parameters and correctly attach them to the re-written URL.
如何将零件与您所知道的事情相匹配取决于您,但是我想这样的事情会很明智:
How you match up the parts with things that you know about is up to you but I imagine that something like this would be sensible:
$knownKnodes = array(
'about',
'projects',
'news',
'join',
);
$knownSubNodes = array(
'the-team',
'project-x',
'the-team'
);
$node = FALSE;
$subNode = FALSE;
$seoLinks = array();
if(isset($part1) == TRUE){
if(in_array($part1, $knownNodes) == TRUE){
$node = $part1;
}
else{
$seoLinks[] = $part1;
}
}
if(isset($part2) == TRUE){
if(in_array($part2, $knownSubNodes) == TRUE){
$subNode = $part2;
}
else{
$seoLinks[] = $part2;
}
}
if(isset($part3) == TRUE){
$seoLinks[] = $part3;
}
if(isset($part4) == TRUE){
$seoLinks[] = $part4;
}
显然,可以从数据库中提取节点和子节点的列表,而不是对其进行硬编码.您如何根据自由文本匹配已知内容的确切细节完全取决于您.
Obviously the list of nodes and subNodes could be pulled from a DB rather than being hard-coded. The exact details of how you match up the known things with the free text is really up to you.
这篇关于PHP + HTACCESS + mod_rewrite +不同长度的URL段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!