views:

50

answers:

1

Hello,

Using MVC. Is there a way to make any url like .Mysite.com to point to mysite.com/user/

as in http://marwan.mysite.com to point to mysite.com/user/marwan

The controller is user and the action is index which takes marwan as its variable.

One idea i had is to make a custom error page to handel 404 and see if the url starts with somthing like *.mysite.com and redirect to the proper controller. However i am sure there must be a better way to do this!

A: 

Take a look at this answer it may help you out.

http://stackoverflow.com/questions/2262/asp-net-url-rewriting

You can try the following:

RewriteConf %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteRule ^(.*)$    http://mysite.com/user/%1$1
RewriteRule ^/user/([a-zA-Z0-9]+)\.mysite\.com/(.*)  http://mysite.com/user/$1/$2 [NC,L]

What this rule does is first insert the host into the URL path and then strip out the correct values from the host and write it the way you want on the backend.

Nick Berardi
I have tried this but it distrupts the flow of the MVC app as in any url is directed to mysite.com/users/.. is it possible to only make username.mysite.com to point to mysite.com/user/username while any other url like mysite.com/search would work fine? Also is there a way to hide the url? as in keep username.mysite.com in the url. Thank you for your help really appreciate it.
Yeah you just need to wrap the proper configuration rules around the rewrite rule. You may want to try including the mod_rewrite change rule [C] which says if any one of the rules fail they all fail in the chain.
Nick Berardi