HTTP Redirect and 301 redirects to ensure that the URL is prefixed with www

Discussion in 'Windows / IIS' started by Will1968, Nov 2, 2009.

  1. Hi,

    I want to ensure that crawlers and vsitors arriving at my site always use a url prefixed with www e.g. http://www.yourdomain.com and not http://yourdomain.com.

    I thought I could do this using the HTTP Redirect module in the IIS Manager.

    However when I set it up my web pages would not load.

    I followed the example in this link and set everything as follows...

    Redirect requests to this location: http://www.yourdomain.com

    All other boxes were unticked

    Status code: 301

    http://www.trainsignaltraining.com/windows-server-2008-http-redirection/2008-03-27/

    What did I do wrong?

    I am using IIS7

    Will
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    you don't want to use redirect because you'll be redirecting back to itself and causes a loop.

    You'll want to use the URLRewrite module to handle 301 redirect.
     
  3. I thought I had a solution but I seem to postback onto myself when i click on a asp button or link button. I don't have a problem with a standard href link.

    This is what I have for the url rewrite in the webconfig.....

    <rule name="Default Document" stopProcessing="true">
    <match url="(.*)default.aspx" />
    <action type="Redirect" url="{R:1}" redirectType="Permanent" />
    </rule>

    taken from http://blogs.msdn.com/carlosag/arch...iteSEO.aspx?CommentPosted=true#commentmessage

    Will
     
  4. Bruce

    Bruce DiscountASP.NET Staff

    Try switching it to rewrite action.
     
  5. I tried that, just changing it to this...

    <rule name="Default Document" stopProcessing="true">
    <match url="(.*)default.aspx" />
    <action type="Rewrite" url="{R:1}" redirectType="Permanent" />
    </rule>

    It did not seem to do anything.

    The redirect (using my intial rule) did not work when the following code was run (or possibly not run) after clicking an asp link button on the default.aspx page

    Protected Sub cmdShoppingBasket_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdShoppingBasket.Click
    Response.Redirect("~/Shopping-Basket.aspx")
    End Sub

    Its not a problem when there is bog standard href.

    It seems to me the problem occurs when on the default.aspx page there is a postback onto itself which then stops any subsequent code being run.

    Any ideas?
     
  6. Try this:

    Code:
    
    <rule name="Redirect to WWW" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP_HOST}" negate="true" pattern="^(www\.)(.*)$" />
            <add input="{HTTP_HOST}" pattern="(.*)" />
        </conditions>
        <action type="Redirect" url="http://www.{C:0}/{R:1}" redirectType="Permanent" />
    </rule>
    
    
     
  7. Hi,

    Thanks for the reply.

    The code you have suggested just appends www to the fromt of a non www url.

    The issue I have is that I want to remove the default.aspx at the front of my homepage.

    Will
     
  8. I thought you still haven't resolved your original issue, which was to redirect to www. So the issue that you're having is that when someone enters your domain name it goes to the default.aspx?
     
  9. i know its an old thread, but if anyone else has got this problem when using default documents rewrite rule, just add this to your master pages .cs file:

    protected void Page_Load(Object sender, EventArgs e) {
    form1.Action=Request.RawUrl.Replace("default.aspx", "");
    ...

    you must have framework v3.5 SP1 installed for form action property (DASP has it, but you may not at home/work), it prevents the old url appearing in address bar when you do postbacks to the same page (no matter what rewrite rule), and the replace part is there so that default.aspx pages dont postback onto themselves with default documents rewrite rule...
     
  10. Hi,

    Thanks for the reply.

    Good idea. Could be a useful approach for a other yet unthought of scenarios.

    Thanks,

    Will
     

Share This Page