« Previous Next »

Thread: Installed, configured and tested pattern, rules don't work

Last post 07-10-2009 12:28 PM by salzano. 8 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (9 items)

Sort Posts:

  • 07-07-2009, 5:03 PM

    • salzano
    • Not Ranked
    • Joined on 07-07-2009, 8:57 PM
    • Posts 9

    Installed, configured and tested pattern, rules don't work

    What am I missing?

    I built the rewrite rule through the interface, but the webconfig looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="non-www" stopProcessing="true">
                        <match url="^mysite\.com(.*)" />
                        <action type="Redirect" url="www.mysite.com{r:1}" appendQueryString="false" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>      
        </system.webServer>  
    </configuration>

    I am not using any .NET code or Visual Studio to create websites. I work with source code directly in TextPad, and all the files in the directory are .asp

    The www version of the website works fine, but the non-www version returns a 200 of zero byte length. This behavior occured before installing the IIS Rewrite add-on altogether. I am trying to create a rule to make the non-www redirect to the www version of this website.

    Thanks

  • 07-07-2009, 11:39 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 661

    Re: Installed, configured and tested pattern, rules don't work

    The rule will not work because the url pattern will never match the request. Check out this post for an example of how to write a "redirect to www" rule: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/, see tip #3.

    Also, for more details about how to access parts of URL within a rewrite rule refer here: http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/#Accessing_URL_Parts_from_a_Rewrite_Rule

    http://ruslany.net
  • 07-08-2009, 9:01 AM In reply to

    • salzano
    • Not Ranked
    • Joined on 07-07-2009, 8:57 PM
    • Posts 9

    Re: Installed, configured and tested pattern, rules don't work

    Thanks for your quick response.

    ruslany:
    The rule will not work because the url pattern will never match the request.

    There has to be some other problem, because the "Test Pattern" feature in the IIS Rewrite module tells me that www.mysite.com/something does not match the pattern and mysite.com/something does match the pattern.

    Here's a screen shot of that happening: http://twitpic.com/9oiop

  • 07-08-2009, 1:58 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 661

    Re: Installed, configured and tested pattern, rules don't work

    The test pattern feature just tests the pattern agains any input string. It does not know whether the input string is a URL path or a domain name.

    At runtime the rule pattern is always evaluated against the URL path. The domain name is not included in that. For example if you request http://mydomain.com/foo/bar/file.aspx, then the rewrite rule pattern will be evaluated against "foo/bar/file.aspx". In order to check the domain name within the rewrite rule you need to add a condition that checks the {HTTP_HOST}:

    <rule name="WWW" enabled="true" stopProcessing="true">
     <match url=" (.*)" />
     <conditions >
      <add input="{HTTP_HOST}" pattern="^www\.mydomain\.com$" negate=”true” />
     </conditions>
     <action type="Redirect" url=http://www.mydomain.com/{R:1} redirectType="Permanent" />
    </rule>

    http://ruslany.net
  • 07-09-2009, 4:23 PM In reply to

    • salzano
    • Not Ranked
    • Joined on 07-07-2009, 8:57 PM
    • Posts 9

    Re: Installed, configured and tested pattern, rules don't work

    Hey, that makes sense. I followed your advise, and now this website's webconfig looks like this:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="non-www" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^d*********\.com$" />
                        </conditions>
                        <action type="Redirect" url="www.d*********.com{R:1}" appendQueryString="false" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>      
        </system.webServer>  
    </configuration>

     I can't help but notice the slight differences. I have no negate=true, and I don't have the www in the condition pattern because I am trying to match requests that come in without it. I have tried with   <add input="{HTTP_HOST}" pattern="^www.d*********\.com$" />  too, but I still get a blank 200 response on the non-www version of the site.

    This is running on Windows Server Web SP1.

  • 07-10-2009, 2:48 AM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 661

    Re: Installed, configured and tested pattern, rules don't work

    The redirection URL does not look right. Try this:

    <action type="Redirect" url="http://www.d*********.com/{R:1}" redirectType="Permanent" />

    http://ruslany.net
  • 07-10-2009, 9:25 AM In reply to

    • salzano
    • Not Ranked
    • Joined on 07-07-2009, 8:57 PM
    • Posts 9

    Re: Installed, configured and tested pattern, rules don't work

    ruslany:

    The redirection URL does not look right. Try this:

    <action type="Redirect" url="http://www.d*********.com/{R:1}" redirectType="Permanent" />

     

     

    Again, your suggestion makes sense, but didn't help. I can get the www version to work and the non-www returns a zero byte 200 response. If I change the pattern on the HTTP_HOST to match with the www, Firefox tells me I have a redirect loop, which makes sense. I am matching the www version and rewriting it to the www version. However, I can't seem to get the non-www version to do anything but show an empty 200 or error if I mangle the webconfig.

     

    I am going to step back and maybe reinstall some things today. Perhaps I should do as I was with IIS 6.0, and create a separate website profile for the non-www version, and redirect that whole profile to the profile with the www.

     

    Thanks again for your help. 

  • 07-10-2009, 12:04 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 661

    Re: Installed, configured and tested pattern, rules don't work

    If you disable or remove the rewrite rule, are you able to successfully browse to either www or non-www version?

    http://ruslany.net
  • 07-10-2009, 12:28 PM In reply to

    • salzano
    • Not Ranked
    • Joined on 07-07-2009, 8:57 PM
    • Posts 9

    Re: Installed, configured and tested pattern, rules don't work

    Yes, the www version works without the rules.

    I have solved the issue after thinking about our last exchange. I learned that IIS 7 returns an empty 200 response for a domain that resolves at the server but is not bound to any website profile. I was used to the 400 bad request error that is returned by IIS 6 in the same situation, and assumed that somehow my redirect rule was causing the empty 200.

Page 1 of 1 (9 items)
Microsoft Communities