I have a solution writted in C# in VS2010. When I type http://www.mywebsite.com in my browser, I am able to display my website successfully. When I type http://mywebsite.com in my browser, I am able to display my website but with some CSS problems. Because in my code I have some settings file where my website is recognised as 'http://www.mywebsite.com'. That's the story.
My question is: if user is typing http://mywebsite.com is there a way to redirect to http://www.mywebsite.com?
For example, if you type http://google.com it is automatically redirected to http://www.google.com
Thanks.
Add this to web.config:
<rewrite> <rules> <rule name="Redirect mywebsite.com to www" patternSyntax="Wildcard" stopProcessing="true"> <match url="*" /> <conditions> <add input="{HTTP_HOST}" pattern="mywebsite.com" /> </conditions> <action type="Redirect" url="http://www.mywebsite.com/{R:0}" /> </rule> </rules> </rewrite>
In code-behind, you can also use something like (not tested)
if(!Request.Url.Host.StartsWith("www"))
Response.Redirect("http://www.mysite.com/" + Request.Url.LocalPath);
but that would have to be on every page on on the MasterPage (if you have one) or slightly altered in Global.asax