Forum Moderators: phranque
I'm fairly new to mod rewrite but I've read up on it quite a bit over the past weeks. I'm in the process of building a fairly complex site but am striving to make it as simple as possible to generally manage and modify in the future. The path I've chosen is to route every page through index.php like so:
index.php?section=users
index.php?section=user&name=elo
index.php?section=groups
index.php?section=group&name=forest+dwellers
index.php?section=account
etc.
You can probablies guess how I'd like it to rewrite but here it is anyway:
/users
/user/yogi
/groups
/group/potato+eaters
/account
etc.
One main factor I wish to maintain is that of reporting 'true' 404 errors. For example, say someone enters /user/paulo and that user doesn't exist. I could say 'user not found' which is all fair and well but it's not a 404 error because the actual page, index.php, was found with a status of 200. I've seen other sites which report true 404 errors this way.
At the moment I've some embarrassingly simple rules which implement what I want (except for the 404 thing). I figured start out simple and then optimise later on, I'm sure you guys could give me some well needed pointers there. Alas, here's what I have:
RewriteEngine onRewriteBase /
# These rules are hardcoded because if a section was entered that didn't
# exist, the index.php script would have to show a false error 404 page with
# an actual code of 200 (ok).# Account (allow slash or no slash)
RewriteRule ^account(/)?$ index.php?section=account [nc]# Groups (allow slash or no slash)
RewriteRule ^groups(/)?$ index.php?section=users [nc]# Group Page
RewriteRule ^group(/)?$ group [r=301,nc]
RewriteRule ^group/([a-z0-9]+(\+[a-z0-9]+)*)$ index.php?section=group&name=$1 [nc]#etc
Of note is the second rule under #Group Page which means a name can be entered like /group/this+group+name+has+spaces.
If there's no plausible answer for getting true 404 errors (while maintaining the current url) it isn't the end of the world, just a preference. I had tryed '^[a-z]+(/)?$ index.php?section=$1' but then as I mentioned earlier it results in a code 200 and some funny behavior (appending '?section=' to the end of some urls). I only intend for 7 - 10 sections to follow?section= so really, it's not that big of a deal but I would like something I could apply to a set of sections (as they follow the same format). For example:
/users and /user/name and ( /user/name/profile, /user/name/statistics, etc. )
/group and /group/name ( /group/name/profile, /group/name/statistics, etc. )
/events and /event/name ( /event/name/details, /event/name/statistics, etc. )
/location and /location/name ( /location/name/details, /location/name/statistics, etc. )
I was going to use rewritemap to switch the names with ids but in the interests of hassle-free installation and portability I decided to go with parsing it in php.
One further problem I have is with trailing slashes, though I haven't had a crack at it yet because of the current issues.
Thanks in advance.
Dan
Jim