Welcome to WebmasterWorld Guest from 54.226.130.194

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

help with this rewrite

     

Sandro87

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

5+ Year Member



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?

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

Sandro87

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

5+ Year Member



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

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




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

Sandro87

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

5+ Year Member



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

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

Jim