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?
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