Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1288 posts 3921 karma points c-trib
    Feb 15, 2010 @ 14:10
    Dan
    0

    Force www in URL

    Hi,

    A client has requested that their site needs to always contain the 'www' in the URL.  Is this something I can set in the Umbraco configs or does it need to be done externally with the DNS or in IIS?

    Thanks

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 14:18
    Nik Wahlberg
    0

    I would set this is IIS. If you have the ability to do so, create a second Web site (bound to domain.com) that is a permanent redirect to www.domain.com (all docs). This should do it for your. Let us know if you need details on this. 

    See second post in this thread for some tips: http://www.windowshostingasp.net/windows_hosting_forums/showthread.php?t=35

    Thanks,
    Nik

  • Dan 1288 posts 3921 karma points c-trib
    Feb 15, 2010 @ 14:25
    Dan
    0

    Thanks Nik, I'm thinking that it might be difficult to do this with the current host, as it's on a shared package at Softsyshosting.  Is there any way to do it within the confines of Umbraco?  It need only be quite crude e.g. just the homepage so that the main domain 'http://thedomain.com' always redirects to 'http://www.thedomain.com'.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 14:37
    Nik Wahlberg
    0

    Ok, in that case I would create yet another top node (homepage node) in your content tree and bind that to your thedomain.com. Then add a new property on the homepage called umbracoRedirect which simply redirects to www.thedomain.com. So:

    1. Create the second homepage node
    2. "Manage Hostnames" on the two top nodes and add the respective domains
    3. Add umbracoRedirect (textbox) to your homepage docType
    4. Set the umbracoRedirect on your thedomain.com homepage
    I have not tried this, but it should work. Please let me know how it works out.
    Thanks,
    Nik

  • Dan 1288 posts 3921 karma points c-trib
    Feb 15, 2010 @ 15:07
    Dan
    0

    I've set that up but it doesn't seem to redirect.  Ordinarily the 'umbracoRedirect' field would be a content picker as opposed to a text string.  Using a content picker the redirect happens, but only to the non-www homepage not the www homepage i.e. I can't dictate a full URL with the content picker, only the node to redirect to.  Doing it as a content picker with a textstring data type using the full URL in the redirect field makes it not redirect at all.

  • Dan 1288 posts 3921 karma points c-trib
    Feb 15, 2010 @ 15:12
    Dan
    0

    I've just done a little investigating actually and the 'umbracoRedirect' field takes a numerical node id.  If I enter the node id of the www homepage into the textstring umbracoRedirect field it redirects, but again, doesn't do the www part.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 15:15
    Nik Wahlberg
    0

    Right, sorry. You're right that takes a nodeId and not a string. So, you could create a UC that simply does something like this:

    if (Request.Url.ToString().IndexOf("www.")==-1)
    {
        Response.Status = "301 Moved Permanently";
        Response.AppendHeader("Location", "http://www.example.com");
    }

    That should do it...hopefully :)

    Thanks,
    Nik

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 15:16
    Nik Wahlberg
    0

    you may also want to check the location that you're in and 1) only do this on the homepage, or 2) check the rest of the URL and append that to the redirect. 

    -- Nik

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 15:21
    Nik Wahlberg
    0

    Oh, and I just saw that this package was addedd recently. Perhaps you can use this? http://our.umbraco.org/projects/301-moved-permanently

    Cheers,
    Nik

  • Dan 1288 posts 3921 karma points c-trib
    Feb 15, 2010 @ 16:18
    Dan
    0

    Thanks Nik - I couldnt' get the 301 package to work (unsure what data type to use and whether it can even handle absolute URLs or whether it's limited to just the content picker again).  I created the usercontrol and that works fine, so thanks for that.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 16:23
    Nik Wahlberg
    0

    10-4, glad to help.

    -- Nik

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 15, 2010 @ 16:39
    Lee Kelleher
    3

    Just to point out that the 301 redirect package only handles the path part of the URL - it doesn't do anything with the domain - so doesn't help in your problem.

    There's probably a way to achieve the force-www using UrlRewritingNet - but I don't know the syntax off the top of my head.

    The approach you have taken with the code-behind will work ... but if you are after performance gains - I'd recommend moving it to a HttpModule, as it's executed earlier down the ASP.NET page life-cycle / pipeline.  More efficient! ;-)

    Cheers, Lee.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 17:25
    Nik Wahlberg
    0

    Nice point Lee. That would make total sense. 

    Cheers,
    Nik

  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Feb 15, 2010 @ 17:51
    Jason Prothero
    0

    You can do this in IIS fairly easily.  Check out a blog post I wrote on this topic a few months ago:

    "How to 301 Redirect example.com to www.example.com in IIS"

     

    Good luck!

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 18:43
    Nik Wahlberg
    0

    Right, but he didn't have access to this at his host (as noted in the beginning of the thread). Thanks. 

    -- Nik

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 15, 2010 @ 18:57
    Morten Bock
    12

    Just wanted to chime in here.

    It is really easy to do this using the UrlRewriting.config file:

    <add name="domainRewrite"
                virtualUrl="http://example.com/(.*)"
                rewriteUrlParameter="ExcludeFromClientQueryString"
                destinationUrl="http://www.example.com/$1"
                redirect="Domain"
                redirectMode="Permanent"
                ignoreCase="true" />

    An you can also do it the other way

    <add name="domainRewrite"
              virtualUrl="http://www.example.com/(.*)"
              rewriteUrlParameter="ExcludeFromClientQueryString"
              destinationUrl="http://example.com/$1"
              redirect="Domain"
              redirectMode="Permanent"
              ignoreCase="true" />

    This will make the 301 redirect for any url on the site.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 19:07
    Nik Wahlberg
    0

    Thanks Morten, I was hoping someone would chime in with that. Great to know! 

  • bob baty-barr 1180 posts 1294 karma points MVP
    Feb 15, 2010 @ 22:24
    bob baty-barr
    0

    plus one for Morton -- i use this method often!

  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Feb 16, 2010 @ 00:45
    Jason Prothero
    0

    Oops. Sorry!  Saw the IIS in the question and missed that response.

    And thanks Morten for URL Re-writing option.  That rocks.

  • SM 19 posts 41 karma points
    Dec 21, 2011 @ 11:30
    SM
    0

    check this link http://goo.gl/7fxh3

  • Brian 7 posts 27 karma points
    Jan 14, 2013 @ 20:01
    Brian
    0

    I had issues with the solutions above.  The following worked for me in Umbraco 4.9.1 - In the UrlRewriting.config, use this:

    <add name="forcewww" virtualUrl="http\://(?!www\.)" redirectMode="Temporary" destinationUrl="http://www." ignoreCase="true" redirect="Domain" />

    This also carries through any querystring parameters that might have been in the original request.  Depending on your needs, you might prefer to set the redirectMode to Permanent.  Happy coding!

Please Sign in or register to post replies

Write your reply to:

Draft