mod重写 - 有很好的网址,但仍然从数据库动态构建菜单

mod重写 - 有很好的网址,但仍然从数据库动态构建菜单

问题描述:

I am creating my menu dynamically from a database. I started by appending an id to the urls which I used to identify the page in the database and then get all urls from the subpages and so on..

so I had a lot of ids like this www.page.com/somefolder/pagename.php?id=10, where 10 is the id of the page and is the parent_id for a number of other pages.

Now I use mod rewrite to get nicer urls. So the above url is now www.page.com/pagename.php , which is mapped to the original url. My pagenames are all gonna be unique.

The question: Do I have to do this for each and every url, since each url needs an id as query string or is there a better way to combine mod rewrite with dynamically generating a menu?

我正在从数据库动态创建菜单。 我开始在url上添加一个id,我用它来识别数据库中的页面,然后从子页面中获取所有url等等。 p>

所以我有很多id 喜欢这个 www.page.com/somefolder/pagename.php?id=10 code>,其中10是页面的id,是许多其他页面的parent_id。 p>

现在我使用mod重写来获得更好的网址。 所以上面的网址现在是 www.page.com/pagename.php code>,它被映射到原始网址。 我的页面名称都是唯一的。 p>

问题:我是否必须为每个网址执行此操作,因为每个网址都需要一个id作为查询字符串,还是有更好的方法将mod重写与动态生成菜单相结合? p> div >

No, not for every URL, but you will need to do this the other way around.

Turn the rewrite engine on

RewriteEngine on 

Build a path to match against using a dynamic 'case' in brackets..

RewriteRule ^/?somefolder/page/([0-9]+)/?$

.. complete the rule with the rewritten URL, using $1 for the 'case'

/somefolder/pagename.php?id=$1 [L]

Put it all together using [L] to signify the 'last' rule to execute.

RewriteRule ^/?somefolder/page/([0-9]+)/?$ /somefolder/pagename.php?id=$1 [NC,L]

This will rewrite /somefolder/page/12345/ to /somefolder/pagename.php?id=12345