|
Author | Message |
---|
Common Member
Posts : 11 Join date : 2011-06-24
| Subject: How to force WWW ? Wed Jun 29, 2011 6:40 am | |
| I'm new to ASP.NET. I usually work on Linux systems with PHP. What I'd like to do is when someone accesses my site by typing mysite.com[/blahblah] , I change the URL to www.mysite.com[/blahblah]On Linux, I'd just do a mod rewrite in the htaccess file, but there isn't one. Any pointers? Thanks! |
|
| |
MoneyMakerNatzi Member
Posts : 3 Join date : 2011-06-16
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:44 am | |
| Just curious, but why do you want to force www? I think www is unnecessary and redundant since www.example.com and example.com serve the same thing. |
|
| |
HottieBaos Member
Posts : 3 Join date : 2011-06-05
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:46 am | |
| eak not really Eak. They only serve the same thing if 1) IIS is set up to view them as the same thing 2) You havn't got your programs using subdomains Not to mention that Google and most other Search engines see them as completely different domains. and you can be dinged for Idential Content on multiple sites. I know how to do it in Classic ASP but not in ASP.NET but here is the classic ASP function i made to do it. - Code:
-
Function SetBaseDomain(ByVal cDomain, byVal bSet301) dim cUrl if request.ServerVariables("https") = "on" then cUrl = "https://" & request.ServerVariables("HTTP_HOST") & request.ServerVariables("URL") else cUrl = "http://" & request.ServerVariables("HTTP_HOST") & request.ServerVariables("URL") end if if Request.QueryString()<>"" then cUrl = cUrl & "?" & Request.QueryString() End If dim arrURL : arrURL = split(cURL, "/") dim arrDomain : arrDomain = split(arrurl(2), ".") if(instr(lCase(arrurl(2)), lcase(cDomain))=0) then if uBound(arrDomain)<2 then arrurl(2) = " www." & cDomain else arrurl(2) = arrDomain(0) & "." & cDomain end if cUrl = join(arrURL, "/") if bSet301 then Response.Status = "301 Moved Permanently" Response.AddHeader "Location", cUrl Response.End Else response.redirect(cUrl) end if ElseIf uBound(arrDomain)<2 then arrurl(2) = " www." & cDomain cUrl = join(arrURL, "/") if bSet301 then Response.Status = "301 Moved Permanently" Response.AddHeader "Location", cUrl Response.End Else response.redirect(cUrl) end if End If End Function The thing about this function is that it will only apply the force IF THERE IS NO OTHER Subdomain... it will also keep hold of https and querystrings and the like. the 301 allows you to tell it to tell the referrer that they should update their link. Hope that helps |
|
| |
HottieBaos Member
Posts : 3 Join date : 2011-06-05
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:46 am | |
| Oh, incidentally you would call it like this:
Call SetBaseDomain("yourdomain.com",true) |
|
| |
MoneyMakerNatzi Member
Posts : 3 Join date : 2011-06-16
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:48 am | |
| In response to your #1: www is just another subdomain, nothing special. Server admins have been serving the same sites with or with out the www for years. Most non technical people just assume the www is a required part of the domain name, which in most case, it is not. see www.org/" target="_blank" rel="nofollow">http://no-www.org/ Now about the duplicate content point... I think Google, Yahoo, etc. are sophisticated enough to realize www.example.com and example.com are the same thing. Have you checked out Google's webmaster tools? You can set how you want your domain name to appear in their listing. (with or without www) Now on to your question... I do PHP for a living so I doubt I will be much help with the asp.net code. It seems like you have the logic down so you just need to convert it to .net. Do you have an online reference? Have you checked out MSDN? http://msdn.microsoft.com/en-us/library/ms644563.aspxWhat language are you using for your site? C#? |
|
| |
Common Member
Posts : 11 Join date : 2011-06-24
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:49 am | |
| I've inherited this site. Basically a client of mine has all their stuff in PHP/MySQL except for this one site, which is .NET. The code (as is) is ASP.NET using VB for the Code-Behind. (I plan to be moving it to C#... not that I'm anti-VB, but I'm used to C# syntax...) Why do I need to do this? Because it has pages that are accessed through SSL. SSL certificate is secured for www.mysite.com. It gives the wonderful "warning" if you are on mysite.com, which (for me) is not a problem because I can easily see it's OK, but some non-tech users panic and then I lost a customer... So I wanted to enforce it always going to www.mysite.com |
|
| |
HottieBaos Member
Posts : 3 Join date : 2011-06-05
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:50 am | |
| i agree with you that technically there is no difference between www. and no www ... however this is not "technically" this is usage. and in the usage world there is a large difference. Google does in fact see them as different, if you don't believe me that's fine, but i would suggest you go do some reading on it at searchenginewatch.com or similar SEO watch sites. Since I received this information from Google themselves (well, they said so in a conference i attended with them as a speaker) i will continue to believe that there is in fact a reason to use one or the other. You said it yourself Google's webmaster tools give you the option to designate for them which is your primary address... why do you suppose they do that? it is because they are not the same. despite your understanding and mine that they are irrelevant, they are still not the same the fact is that if there are host headers being used on the site then specifing www is also manditory, www is a subdomain and as such, if you do not set up IIS to accept *.mysite.com then you have to specify all possible subdomains. then of coarse there is the reason that Danial mentioned. SSL certs, and registration codes for many ISAPI, JAVA, ActiveX &tc. apps see sites as very different depending on the existence of the www. [example Aurigma Image Uploader] many sites require you to put in the www... if you attempt to go to the site without the www. you will get a connection error. True, this is just poor setup by an inexperienced sys admin, but the fact that there is a setup which will prevent this belies the thought that they are the same. anyway... all the wanting in the world does not change the fact that with or without www are not the same thing. they should be, but they are not. |
|
| |
Drake Member
Posts : 5 Join date : 2011-05-20
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:51 am | |
| I think what you want is URL Rewriting, but it is a pain to do in dot net. Here is a link. http://msdn.microsoft.com/en-us/library/ms972974.aspxHave you thought about maybe when someone visits your site set a session then redirect then to the www.mysite.com. Then look and see if the session is there if it is then you know they have already been redirected and should be showing the www.You would be forcing a page load two times per visiter. May not be the best idea but you can always see what you can do with the url rewrite. |
|
| |
Common Member
Posts : 11 Join date : 2011-06-24
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:53 am | |
| I'm playing with the idea of doing the rewriting. It's actually not that bad because there's already a few HTTPModules in place for that, but what I was hoping was that the www enforcement would show in the browser (in the URL), but not my internal rewriting (which does not show). |
|
| |
noaeg Member
Posts : 3 Join date : 2011-05-11
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:55 am | |
| Can't help with VB, but it sounds like you are familiar with both so shouldn't have any problems converting it. The most straight forward way to do this is extend the page base class, and add a method like this: - Code:
-
private void ForceWww() { string www = "http://www.yourdomainname"; string noWww = "http://yourdomainname";
Response.Redirect(Request.Url.ToString().Replace(noWww , www)); }
All you're doing here is examining the request when it comes in, then redirecting if the www is missing. Then call ForceWww() in the base page class OnInit event. Remember to call base.OnInit when overriding onInit though! - Code:
-
protected override void OnInit(EventArgs e) { base.OnInit(e); ForceWww(); }
While you can discuss the pros and cons of doing something all day long, sometimes the client just needs it, so you just have to do it :D |
|
| |
Flippers Member
Posts : 1 Join date : 2011-05-11
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:56 am | |
| While you can discuss the pros and cons of doing something all day long, sometimes the client just needs it, so you just have to do it :D Excellent Point. Question: could you have used - Code:
-
Response.Status = "301 Moved Permanently" Response.AddHeader "Location", cUrl Response.End
to do that as a 301 redirect? IF so, i would suggest that, so that any improper links to the page will be updated. |
|
| |
noaeg Member
Posts : 3 Join date : 2011-05-11
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:57 am | |
| Excellent Point. Question: could you have used - Code:
-
Response.Status = "301 Moved Permanently" Response.AddHeader "Location", cUrl Response.End
to do that as a 301 redirect? IF so, i would suggest that, so that any improper links to the page will be updated. [/quote] Yep, can't see see any reason why not. By the way hot avatar |
|
| |
PrettyBoyBlue Member
Posts : 1 Join date : 2011-04-17
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:58 am | |
| Achieving consistency in your urls is important. See http://www.mattcutts.com/blog/seo-ad...onicalization/ at Matt Cutts' blog for more information on why this is important. From an SEO perspective, using one form will help search engines understand which is the standard form you want it to recognize. A www and a non-www site can be seen as two different sites - bad for SEO. 301 redirects are one way to do it. Another possibility is to add redirection for the site either through the IIS Admin panel or from the command line to the IIS metabase. Try a Google search on : iis force redirection to www If the site is on a shared hosting plan, you can't do this. If you control the server, this will work. Good luck |
|
| |
Yola Member
Posts : 12 Join date : 2011-03-09
| Subject: Re: How to force WWW ? Wed Jun 29, 2011 6:59 am | |
| There is another way I do not see mentioned. Can be accomplished as such:
If you wire your asp.net page code behind definitions to a BasePage class inheriting from Web.UI.Page rather than have your pages inherit directly from UI.Page then you can create a single method to test the Request like so:
if (Request.Url.AbsoluteUri.IndexOf("www") == -1 && Request.Url.Host.IndexOf("secure.mysecuredomain.com") == -1) { Response.Redirect("http://www.mydomain.com"); return; }
above would go in forms authentication login page.
then let every other page that inherits from your new base page do the following:
Call TestIfWWWAbsentWithoutLeave()
let implementation be like so:
if (Request.Url.Host.IndexOf("//mydomain.com") > -1 && Request.Url.Host.IndexOf("secure.mydomain.com") == -1) { string s = Request.Url.AbsoluteUri;
string s2 = s.Replace("http://mydomain.com", "http://www.mydomain.com"); Response.Redirect(s2); return; }
Now is it easier to rig IIS? maybe but what if you port to a new machine and you forget to rig that server? at least this way the code will keep the www present IF you make all your asp.net pages inherit from basepage.
Additionally once they do inherit from a basepage than you can code common functions like, isUserAuthenticated and so on in one convenient location. And then if you change all pages inheriting from this basepage will pick up changes thus leveraging the OO nature of C#. |
|
| |
Sponsored content
| Subject: Re: How to force WWW ? | |
| |
|
| |
|