Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite on specific directories

         

ladams02

6:19 pm on Nov 27, 2006 (gmt 0)

10+ Year Member



Sorry to post another mod_rewrite thread as I know there are many others out there (I've read through lots), but I feel like every other example I look at and try does not work for me!

I'm trying to only conduct mod_rewrites on folders that contain a specific string "-community". So /office-community/ should use the redirects where as /timothy/ should not.

Here is my condition:


RewriteCond %{THE_REQUEST} ^[A-Z]+[-community]

But if I goto /timothy/ it is still attempting to redirect me. Please advise and many thanks!

jdMorgan

6:27 pm on Nov 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post the RewriteCond and its associated RewriteRule, so we don't have to guess.

Jim

ladams02

6:37 pm on Nov 27, 2006 (gmt 0)

10+ Year Member



Well theres currently 39 rules attached to this condition. If it is necessary to post them all I will, but here is the basic layout of what the rules consist of.

RewriteRule ^([^/]+[-community])/?$ /community_luke/browse.php?category=$1 [L]
RewriteRule ^([^/]+[-community]+)/login/?$ /community_luke/login.php [L]
RewriteRule ^([^/]+[-community]+)/register/?$ /community_luke/signup.php [L]

jdMorgan

7:38 pm on Nov 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since this is an internal rewrite and not an external redirect, I see no need to reference the original client request using RewriteCond and THE_REQUEST.

You should be able to get the required function simply by correcting your RewriteRule patterns. Something like:


RewriteRule ^([^-]+-community)/?$ /community_luke/browse.php?category=$1 [L]
RewriteRule ^([^-]+-community)/login/?$ /community_luke/login.php [L]
RewriteRule ^([^-]+-community)/register/?$ /community_luke/signup.php [L]

Used in a regular-expressions pattern, [abc]+ matches one or more characters equal to a, b, or c, in any combination. Therefore [-community]+ means "match one or more characters equal to hyphen, c, o, m, u, n, i, t, or y, in any combination" -- not likely what you wanted.

You may also exclude specific characters, as we did above with [^-]+ meaning, "Match one or more characters NOT equal to a hyphen," or equivalently, "Match up to the first hyphen."

Also be aware for future reference that a RewriteCond affects only the first RewriteRule that follows it. If you wish a condition to affect more than one rule, you must construct a negative-logic rule that skips over those multiple rules if that condition is NOT true.

For example:


# If URL does NOT contain "-community/", then leave it alone and skip the next 39 rules
RewriteRule !-community/ - [S=39]
#
RewriteRule ^([^-]+-community)/?$ /community_luke/browse.php?category=$1 [L]
RewriteRule ^([^-]+-community)/login/?$ /community_luke/login.php [L]
... 37 more rules to be skipped
#
# unconditional code execution resumes below this line

This solution appears to be unnecessary in the present case.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

ladams02

3:56 am on Nov 30, 2006 (gmt 0)

10+ Year Member



Sorry for the late reply but many thanks for helping me with this. I'm still trying to get more familiar with mod_rewrite and reg expressions and your help was greatly appreciated!