1

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

as in http://marwan.example.com to point to example.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 handle 404 and see if the url starts with something like *.example.com and redirect to the proper controller. There must be a better way to do this!

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

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

ASP.NET URL Rewriting

You can try the following:

RewriteConf %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$    http://example.com/user/%1$1
RewriteRule ^/user/([a-zA-Z0-9]+)\.example\.com/(.*)  http://example.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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
  • 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. –  Oct 01 '09 at 10:15
  • 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 Oct 01 '09 at 23:48