Rewrite where a pattern doesn't match

Topics: Developer Forum, User Forum
Feb 19, 2007 at 11:24 PM
I know this is simple, but I can't get it to work. I want to match any path, /XXX/(anything) and replace it with /client/(anything). This works fine with this:

RewriteRule /(a-zA-Z0-9)+/(.*) /client/$2 U

However, if someone goes to /blogadmin/(anything), I DON'T want to rewrite. I tried this:

RewriteCond %{PATH_INFO} !(^/blogadmin/)

But while it lets /blogadmin/ work, it breaks the /XXX/ to /client rule.
Feb 20, 2007 at 3:05 PM
I'm sure this isn't the best way, but you could always do something like this:

RewriteRule /blogadmin/(.*) /blogadmin/$1 U,L
RewriteRule /(a-zA-Z0-9)+/(.*) /client/$2 U

The first rule matches the blogadmin, and the L flag tells the rewrite engine to stop and process no further.

Everything else drops down to the 2nd rule, which is what you want...
Feb 22, 2007 at 4:59 PM
So this is working, except I noticed something odd.

I can go to XXXX/foo/index.cfm and it works. But if I go to XXXX/foo/admin it rewrites in the browser to XXXX/client/admin. I need it to say XXXX/foo/admin.

However - if I go to XXXX/foo/admin/ it works fine.

Make sense?
Feb 22, 2007 at 5:55 PM
Okay, so try this:

RewriteRule /blogadmin/(.*) /blogadmin/$1 [U,L]
RewriteRule /[a-zA-Z0-9]+/([^\.]+)\.(.*) /client/$1.$2 [U]
Feb 22, 2007 at 7:09 PM
Nope, no good. What is with the .? I did have a . in my entry, but only as the end of the sentence.
Feb 22, 2007 at 8:54 PM
Edited Feb 22, 2007 at 8:55 PM
The rules I provided should work.

The 2nd rule is the most important.

/a-zA-Z0-9+/ - This looks for any subfolder name.

(^\.+)\.(.*) - This will match on any script name - index.asp, index.cfm, index.html, etc. - and split it into two parts. $1 will contain the name, and $2 will have the extention.

If you have a subfolder like /foo/admin it will not match the rule, since it doesn't contain a period.

Check your log file to get a clue at what might be going wrong.

One last tip -- this web site will help you test your RegEx expressions: