Page 1 of 1

mod_rewrite rewrite

Posted: Wed Nov 01, 2006 8:37 pm
by m3mn0n
Is this the most effective method for doing this rewrite? Is there an easier way (perhaps, obvious to experts) that I've not been able to come across? Don't get me wrong, it works and I'm happy it does but if there is a more effective way to do this, I'd love to hear it.

Code: Select all

RewriteEngine on
RewriteRule ^/(main|about|services|etc)/(.+)/(.+)$ /index.php?section=$1&page=$2&sub=$3 [QSA,L]
RewriteRule ^/(main|about|services|etc)/(.+)$ /index.php?section=$1&page=$2 [QSA,L]
RewriteRule ^/(main|about|services|etc)/$ /index.php?section=$1 [QSA,L]
RewriteRule ^/(main|about|services|etc)$ /index.php?section=$1 [QSA,L]
To explain the obvious: basically I'd like the site to have clearner urls with three different levels of rewriting which would in turn go to the variables $section, $page, and $sub.

At first I figured just adding the first rewrite rule would work, but if you go to any page with just 2 levels of rewriting, eg. /main/home/ it would cause an error where as /main/home/bla/ would be fine.

I was stumbled being my first time playing around with this, so I did some more research for a few hours and i couldn't find an easy solution. So I did the same rule without the 3rd level on the next line. And presto, the 2nd level url worked. Then the single level urls would give errors. Easy solution, another line with just 1 level. But then came the issue of single level urls causing errors if there was no trailing slash. So I added the 4th line.

So everything works. Just like I want. But is there an easier/more efficient way?