views:

50

answers:

0

My goal is to redirect:

http://users.domain.com/<username>/other/path/info

to

http://users.domain.com/other/path/info

The key here is that that the "username" in the source URL could of course be anything.

Here is a more concrete example of what I want to do (and why):

Suppose I have username of "bill". I want Bill to be able to go to http://users.domain.com/bill and see a customized version of what would normally be present at http://www.domain.com.

Similarly, as bill navigates around his site, I want the URL to stay as his "custom URL", for example:

http://users.domain.com/bill/products
http://users.domain.com/bill/promotions
http://users.domain.com/bill/coupons
etc

Would need to map to:

http://users.domain.com/products
http://users.domain.com/promotions
http://users.domain.com/coupons

I will worry internally (via PHP) about checking the URI to extract "bill" to know that I need to visually brand the web site for Bill -- that is after the URL rewriting works though, so not a concern here in this post.

Additionally, the URLs could have more than just 1 directory after the user name, for example:

http://users.domain.com/bill/products/computers/monitors

would need to map to

http://users.domain.com/products/computers/monitors

This is really where my hours of rewrite troubleshooting have been focused. Note that in the URL above, "products/computers/monitors" itself needs to ultimately be rewritten (e.g., to products.php?category=computers&subcategory=monitors or whatever). I already have rules to do this rewriting which work fine for the main www.domain.com site.

I have tried this rewrite code:

RewriteCond %{HTTP_HOST} ^users\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z_0-9\-]+?)/(.+)$ $2 [NC,L]

This supposedly will rewrite USERNAME/OTHER/PATH/INFO to OTHER/PATH/INFO, where "USERNAME" only consists of letters, numbers, hyphens, or dashes.

The problem is that this creates a rewrite loop. Why? Consider again the source URL that we need to rewrite:

http://users.domain.com/bill/products/computers/monitors

On the first pass, it will take "bill/products/computers/monitors" and will rewrite to "products/computers/monitors". So far so good -- but the problem then happens on the next rewrite. It takes the result of the first rewrite, "products/computers/monitors", and rewrites it to "computers/monitors", then again to just "monitors". This is why it's important to note that there could be multiple "directories" after the username. With just one "directory" (like "bill/products"), it appears to work fine because it just rewrites "bill/products" to "products" which is what I want. The issue only rears itself if there is more than one path part after the username, which would of course be quite common on a regular site.

What I WANT to do is just go through this rule ONCE, and then after that it'd not go through this rule and would just be processed by the regular rewrite rules against "products/computers/monitors".

How do I achieve this?

I have tried this solution which has been suggested:

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

But this does NOT work because it stops after the very first redirect, so the real rules to match "products/computers/monitors" after the first redirect never get a chance to run.

I have also tried setting environment variables on the first redirect and then NOT doing the redirect if the variable is present, but this hasn't worked either. E.g.,

RewriteCond %{ENV:REDIR} !yes
RewriteRule ^([a-zA-Z_0-9\-]+?)/(.+)$ $2 [NC,L,E=REDIR:yes]

I have also tried matching on %{THE_REQUEST} in a RewriteCond, but this doesn't work because my usernames are dynamic (all example I've see of that working are for when you know what the bit is you're trying to filter out).

Does anyone have any suggestions here? I'm going on 7-8 hours of research and trial and error with little ultimate success...

Thanks!