« Previous Next »

Not Answered Thread: URL rewrite on localhost

Last post 07-10-2009 1:33 PM by dbalasko. 18 replies.

Average Rating Rate It (5)

RSS

Page 1 of 2 (19 items) 1 2 Next >

Sort Posts:

  • 05-12-2009, 4:02 PM

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    URL rewrite on localhost

    I am developing a web application which should have the following rewriting:

    user.domain.com
    domain.com/user.aspx?name=user

    This should be the rewrite rule I believe:

    <rewrite>  
                <rules>
                    <rule name="subdomain">
                        <match url="([a-zA-Z]+)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^[^.]+\.domain\.com" />
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="users.aspx?name={R:1}" />
                    </rule>
                </rules>
            </rewrite>

     

    But I can't test it on localhost, I tried with this pattern:

    "^[^.]+\.localhost"

    But it doesn't work. Is there a way to test this rewrite on localhost or can this work only in production environment?

  • 05-12-2009, 4:10 PM In reply to

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

    Re: URL rewrite on localhost

    You can try updating the \windows\system32\drivers\etc\hosts file with as below:

    127.0.0.1         user1.domain.com
    127.0.0.1         user2.domain.com

    Then you should be able to request http://user1.domain.com in brower.

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

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    Re: URL rewrite on localhost

    It works now, thank you.
    Now I still need a rule for http://user1.domain.com/pictures for example, it should be rewriten to

    http://domain.com/user.aspx?pictures=1&user=user1

    I tried with:
    <rule name="SubDomainPictures" stopProcessing="false">
                        <match url=".*" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^([^.]+)\.domain\.com\pictures" />
                        </conditions>
                        <action type="Rewrite" url="user.aspx?pictures=1&amp;name={C:1}" />
                    </rule>

     

  • 05-12-2009, 5:06 PM In reply to

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

    Re: URL rewrite on localhost

    {HTTP_HOST} contains only the host name, e.g. "user1.domain.com". The /pictures is the URL path and it should be matched by the <match> element of the rule.

    <rule name="SubDomainPictures" stopProcessing="false">
                        <match url="^pictures" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^([^.]+)\.domain\.com$" />
                        </conditions>
                        <action type="Rewrite" url="user.aspx?pictures=1&amp;name={C:1}" />
                    </rule>

    http://ruslany.net
  • 05-12-2009, 5:39 PM In reply to

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    Re: URL rewrite on localhost

    I have tried it, but it doesn' work. I have tried with some modifications:

    <rule name="SubDomainPictures" stopProcessing="false">
                        <match url="domain.com/pictures$" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^([^.]+)\.domain\.com" />
                        </conditions>
                        <action type="Rewrite" url="user.aspx?pictures=1&amp;name={C:1}" />
                    </rule>

    I have changed the match condition because in your case ("^pictures"), the url should start with "pictures" but it doesn't, it comes at the end (http://user1.domain.com/pictures).
    I also removed the "&" sign in the pattern since there is "/pictures" after ".com".

    But it still doesn't work !?
    When I test it in IIS url rewrite module, both url match and pattern pass.

  • 05-12-2009, 10:22 PM In reply to

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

    Re: URL rewrite on localhost

    The patttern that you have specified will not work because the <match> element evaluates the URL path only. The URL path will never contain domain.com hence the pattern will never match.

    Take a look at this article that describes how the URL parts are accessed in a rewrite rule: http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/#Accessing_URL_Parts_from_a_Rewrite_Rule 

    The rule that I gave before should work. When you say it does not work - does the request to http://user1.domain.com/pictures result in 404 or it gets rewritten but the pictures=1 is not there?

    http://ruslany.net
  • 05-13-2009, 12:26 PM In reply to

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    Re: URL rewrite on localhost

     No, it doesn't result in 404, it just isn't there.

  • 05-13-2009, 12:39 PM In reply to

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

    Re: URL rewrite on localhost

    Do you have any other rules or is that the only rule you have? If you have another rule before the one for the /pictures, then it may be possible that preceeding rule rewrites the URL and it never reaches the /pictures rule.

    http://ruslany.net
  • 05-13-2009, 2:03 PM In reply to

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    Re: URL rewrite on localhost

    That's it, the rule "subdomain" from my 1. post was the problem.

    I simply moved the "SubDomainPictures" rule up and marked "Stop processing of subsequent rules".

    Works now, thank you very much.

  • 07-08-2009, 6:56 AM In reply to

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    Re: URL rewrite on localhost

    I'm back.
    I am testing the whole thing on the production server now and I have some problems.

    So the subdomain rule I am using is:

    <rule name="subdomain">
                        <match url="([a-zA-Z]+)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^[^.]+\.domain\.com" />
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="users.aspx?name={R:1}" />
                    </rule>

    But when I try with for ex. test.domain.com, I get the IIS7 default welcome page.

    Any ideas?

  • 07-08-2009, 8:37 AM In reply to

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    Re: URL rewrite on localhost

     I think I know why I allways got IIS welcome page, it was due to configuration of the DNS zone:

     *        A    xxx.xxx.xxx.xxx

     I removed that line, now I don't get IIS welcome page any more, but it still doesn't work with the above rule.

  • 07-08-2009, 2:05 PM In reply to

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

    Re: URL rewrite on localhost

    In the previous rule example you used subdomain as a query string parameter, e.g.:

    users.aspx?name={C:1}

    Now, you have changed that to

    users.aspx?name={R:1}

    Is this an intended change?

    http://ruslany.net
  • 07-09-2009, 7:40 AM In reply to

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    Re: URL rewrite on localhost

    If you check my first post in this topic, it was {R:1}. Anyway, I tried both but it still doesn't work.

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

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

    Re: URL rewrite on localhost

    The only thing I can suggest is to try using Failed Request Tracing to see if/how the rule is applied.

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

    • dbalasko
    • Not Ranked
    • Joined on 10-02-2006, 10:02 AM
    • Posts 11

    Re: URL rewrite on localhost

     I tried that but nothing gets written in the log. Other rules do, like this one for ex.

    <rule name="Rewrite to article.aspx">
              <match url="^article/([0-9]+)/([_0-9a-z-]+)" />
              <action type="Rewrite" url="article.aspx?id={R:1}&amp;title={R:2}" />
            </rule>

    So I believe I have set the tracing Ok.

    I simply get the error "Server not found...".

    After that I returned the following line to the DNS zones configuration:
     *        A    xxx.xxx.xxx.xxx
    Without it, there were problems, for ex. pop.domain.com didn't work anymore.

    No I am getting the IIS welcome page again for the subdomain rule and nothing gets written in the logs. The Article rule works fine.

    As if this request never gets to the point where the rule should be applied. Could there be something else in the IIS configuration that would block subdomain requests?

Page 1 of 2 (19 items) 1 2 Next >
Microsoft Communities