homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

help with this rewrite

 5:33 pm on Mar 15, 2010 (gmt 0)

hello I've created this rule

RewriteRule ^(news|info)/(((([a-z]*)/?)?(([a-z-]*)/?)?)?(([a-z]*)/?)?)? index.php?section=$1&cat_1=$5&cat_2=$7&cat_3=$9

that should rewrites links that start with site.com/news/ OR Info/ and could have up to three other cat after them, but are not mandatory after news or info and can be only 1 2 or 3 and could end with a / or without.

This works it seems. the problem is that if I put More than three cat doesn't give a 404 error! I want that once it gets a forth element (news/cat1/cat2/cat3/cat4 <- not allowed) it won't rewrite! Is it possible?



 6:14 pm on Mar 15, 2010 (gmt 0)

Add the [L] flag to the end of the rule.

Adding a $ end anchor to the pattern will force three or less.

There's a potential problem when only one element is present. Will it match the first pattern or the last pattern? Will it do that on all servers, and Apache versions? Could it change after a server upgrade?

Another problem comes from the * in the
[a-z-]* pattern, and the optional slashes mid-URL. A request like example.com/something[b]//[/b]something is perfectly valid (but highly undesirable) with your rules.

I think I would use three rules, with one, two and three fixed patterns.

Don't make the trailing slash optional. Either require it, or require that it be omitted, otherwise you're introducing yet more Duplicate Content issues.


 7:56 pm on Mar 15, 2010 (gmt 0)

Hey thanks for your help, I think you're right so i did this. Tell me if you see something potentially wrong

RewriteRule ^(news|info)$ index.php?section=$1
RewriteRule ^(news|info)/([a-z-]+)$ index.php?section=$1&cat_1=$2
RewriteRule ^(news|info)/([a-z-]+)/([a-z-]+)$ index.php?section=$1&cat_1=$2&cat_2=$3
RewriteRule ^(news|info)/([a-z-]+)/([a-z-]+)/([a-z-]+)$ index.php?section=$1&cat_1=$2&cat_2=$3&cat_3=$4


 8:00 pm on Mar 15, 2010 (gmt 0)

Add the [L] flag to the end of each rule. :)


 8:34 pm on Mar 15, 2010 (gmt 0)

Thank you, very helpful :)

Just curious why?... You said it will prevent continuing rewriting? But the "$" at the end of the pattern seems to work alone


 8:40 pm on Mar 15, 2010 (gmt 0)

The [L] flags ends processing by Mod_Rewrite there and then.

Always include it unless you know a very good reason to omit it.


 3:19 pm on Mar 16, 2010 (gmt 0)

The [L] flag ends mod_rewrite processing if the rule is invoked.

Since it is unlikely that you would want your server to have to test all of the following rules after matching and invoking a particular rule, this is a question of efficiency. The best way to avoid prematurely-required server upgrades is to avoid wasting CPU cycles, so the [L] flag is almost always a good idea. By the time you need code that you wouldn't want to use the [L] flag on, you'll know why you don't want to use it, as it's generally only not wanted for certain "advanced" coding techniques.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved