dongzong2017
dongzong2017
2015-07-27 19:09
浏览 23
已采纳

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpmopn8542
    dpmopn8542 2015-07-27 19:49
    已采纳

    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

    点赞 评论

相关推荐