htaccess的:重写无限的子目录 [英] htaccess: rewriting unlimited subdirectories

查看:205
本文介绍了htaccess的:重写无限的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的内容管理系统,我用PHP编写的,具有无限的层次类别。

每个类别都有一个短名称(从类别标题动态生成的),我使用带有mod_rewrite的生产清洁的网址,例如:

http://www.mysite.com/categories/january

使用这种方法的问题是,每个类别需要唯一短名称为它的正常工作。这就产生了一个问题,当我有多个类别名称相同,具有不同的类别,例如: /类别/ 2011 /一月/类别/ 2010 /月。

要克服这一点的最简单方法是追加-1,-2等创建了重复短名称的时候,但我更preFER有在URL中使用的全品类的层次结构,并确定合适的IDS基于这一点,比如

http://www.mysite.com/categories/2011/january VS http://www.mysite.com/categories/2010/january

我的问题是:有没有我可以使用mod_rewrite来解析任意数量的子类别,并返回这些给我的PHP页面,也许值的数组

办法?

有需要的子类别的数量,无论工作,所以它可以处理例如

http://www.mysite.com/categories/products/鸡舍/报告/ 2011 /一月

与同code,并给我的PHP页面的这些类别的顺序显示在URL数组?

我的code然后将需要从最后一类反向工作,找出哪些类别是根据它的父母要求的。

这是什么,这将是(建议和确实)是可行的,而且任何人都可以给我一个手产生正确的正前pression的改写指令,给我的pretty的无望掌握正则表达式的<? / P>

在预先感谢 尼克

解决方案

我不认为有什么简单的方法来让mod_rewrite的将数据拆分为您服务。当然,你可以建立一个重写规则匹配多达9个虚拟路径,但我不认为这将是有益的。相反,我建议像这样定义的规则

 重写规则^类/(.*)$类,handler.php?猫= $ 1
 

然后做分裂(和重新排序)在PHP文件中,这样的事情

  $猫= $ _GET ['猫'];
$ splittedCat = array_reverse(preg_split('/ \ //',$猫));
 

I have a fairly complex content management system I've written in PHP, which has unlimited hierarchical categories.

Each category has a shortname (dynamically generated from the category title) which I using with mod_rewrite to produce clean URLs, for example

http://www.mysite.com/categories/january

The problem with this approach is that each category requires a unique shortname for it to work properly. This creates a problem when I have more than one category with the same name, with different categories, e.g. /categories/2011/january and /categories/2010/january.

The easiest way to overcome this is to append -1, -2 etc when a duplicate shortname is created, but I would much prefer to have the whole category hierarchy used in the URL and determine the right Ids based on this, e.g.

http://www.mysite.com/categories/2011/january vs http://www.mysite.com/categories/2010/january

My question is this: Is there a way that I can use mod_rewrite to parse any number of subcategories, and return these to my php page, perhaps as an array of values?

It would need to work regardless of the number of subcategories, so it could process e.g.

http://www.mysite.com/categories/products/sheds/reports/2011/january

with the same code, and give my php page an array of those categories in the order they are shown in the URL?

My code would then need to work backwards from the last category to figure out which category is required based upon it's parents.

Is this something that would be feasible (and indeed recommended), and could anyone give me a hand with producing the correct regular expression for the rewrite command, given my pretty hopeless grasp of regex?

Many thanks in advance Nick

解决方案

I don't think there is any easy way to let mod_rewrite split the data for you. Of course, you could build a RewriteRule that matches up to 9 virtual paths, but I don't think that this would be helpful. Instead I would suggest to define a rule like this

RewriteRule ^categories/(.*)$ category-handler.php?cat=$1

and then do the splitting (and re-ordering) in the PHP file, something like this

$cat = $_GET['cat'];
$splittedCat = array_reverse(preg_split('/\//', $cat));

这篇关于htaccess的:重写无限的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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