views:

70

answers:

1

I have a web app which serves images based on the subdomain.

We wanted to provide our users with a url like this: http://{username}.domain.com/images/myimage.jpg

Instead of what we used to have: http://www.reallylongdomainname.com/users/{username}/images/myimage.jpg

This makes the url shorter and less 'snoopable'.

So I set up an IIRF .ini file to do some url rewriting and it works great except for the fact that some of our users folders have an underscore. And from what I've read, underscore is not a valid character in a domain name (even though IIS supports it).

I want to know how I could do a find and replace in the $1 back reference so that a url like this:

http://some-user.domain.com/...

Could be rewritten to this:

/users/some_user/..

Here's my IIRF rule.

RewriteCond %{HTTP_HOST} ^(?!www)([^\.]+)\.domain\.com
RewriteRule ^/(.*)$   /users/*1/$1 [L,I]

Thanks for any help.

A: 

If you know there is no more than x dashes in the username:

# no dash
RewriteCond %{HTTP_HOST} ^(?!www)([^\.\-]+)\.domain\.com
RewriteRule ^/(.*)$   /users/*1/$1 [L,I]

# one dash
RewriteCond %{HTTP_HOST} ^(?!www)([^\.\-]+)-([^\.\-]+)\.domain\.com
RewriteRule ^/(.*)$   /users/*1_*2/$1 [L,I]

# two dashes
RewriteCond %{HTTP_HOST} ^(?!www)([^\.\-]+)-([^\.\-]+)-([^\.\-]+)\.domain\.com
RewriteRule ^/(.*)$   /users/*1_*2_*3/$1 [L,I]

It's not beautiful, but it works.

marapet