Need help creating a rewrite rule [Answered]RSS

10 replies

Last post Jul 30, 2009 07:22 PM by rykn

  • Need help creating a rewrite rule

    Jul 29, 2009 09:35 AM|jakleci|LINK

    hey guys, how can i accomplish the following as simple as possible. i heard i can do this very easily on IIS7 with this URL Rewrite Module but i have never used it so i am asking for your assitance.

    basically i need to rewrite short user profile URLS into an aspx page like this:

    http://site/NICK => http://site/profile.aspx?nick=NICK

    the problem is that i want any other requests to be ignored, like requests to .aspx files, images, folders and such for example:

    i want these to get handled:
    site.com/john => site.com/profile.aspx?nick=john
    site.com/mark => site.com/profile.aspx?nick=mark
    site.com/fred => site.com/profile.aspx?nick=fred

    i DONT want these to get handled:
    site.com/page.aspx
    site.com/images/logo.jpg
    etc

    your assistance is appreciated

  • Re: Need help creating a rewrite rule

    Jul 29, 2009 09:50 AM|Stephen Hewison|LINK

    How this is normally handled is by registering a new virtual directory when your user's profile are created.

    The virtual directories will all point to the same physical folder.  You can then interigate the url get the username.

    There is a code project example that will explain the process:

    http://www.codeproject.com/KB/cs/virtualdir.aspx

    Other ways would be to implement a custom http hander.  Although this might be bit more difficult.  You would then have a sample url such as:

    site.com/john.profile

    You can find out information on custom http handlers at:

    http://support.microsoft.com/kb/308001

  • Re: Need help creating a rewrite rule

    Jul 29, 2009 09:59 AM|jakleci|LINK

    obviously i dont want to create hundreds of thousands of these virtual directories, one for each user ;)

    so you are saying that this is impossible to accomplish with the URL Rewrite Module?

    thanks

  • Re: Need help creating a rewrite rule

    Jul 29, 2009 02:37 PM|ruslany|LINK

    This is relatively easy to do with URL Rewrite module. Rule may look something like this:

    <rule name="userprofile">
           <match url="(\w+)" />
           <conditions >
             <add matchType="IsDirectory" negate="true" />
             <add matchType="IsFile" negate="true" />
           </conditions>
           <action type="Rewrite" url="profile.aspx?nick={R:1}" appendQueryString="false" />
    </rule>

    The conditions are used to check that the requested url does not correspond to a file or a folder on a server's file system. This basically prevents the rule from rewriting requests to you existing aspx pages and static files.

    You will need to place this rule into the web.config file located in the same folder where profile.aspx file is. Place the XML inside of the <configuration>/<system.webServer>/<rewrite>/<rules> section.

     

  • Re: Need help creating a rewrite rule

    Jul 29, 2009 06:09 PM|jakleci|LINK

     thank you, it almost works :)

     i say almost as the web services seem to be not working now, can you help?

     i really appreciate your help

  • Re: Need help creating a rewrite rule

    Jul 29, 2009 06:53 PM|jakleci|LINK

    false alarm, it was rewriting the .axd's as well, so i included the following and it works :)

  • Re: Need help creating a rewrite rule

    Jul 30, 2009 06:26 PM|Rykn|LINK

    I have a similar issue, however no matter what I try I cannot get ANY rule to work on my site. My URL looks like this -> http://localhostmyapp/genericpage.aspx?pid=west, but I want it to look like this -> http://localhost/myapp/west/genericpage.aspx I am running IIS7 on server 2008 standard ed. Please help.
  • Re: Need help creating a rewrite rule

    Jul 30, 2009 06:43 PM|ruslany|LINK

    Please post the rewrite rules that you have (they are in the web.config file inside of the <rewrite> section).

  • Re: Need help creating a rewrite rule

    Jul 30, 2009 06:52 PM|Rykn|LINK

            <rewrite>
                <rules>
                    <rule name="RedirectUserFriendlyURL1" patternSyntax="ECMAScript" stopProcessing="true">
                        <match url="^myapp/genericmain\.aspx$" ignoreCase="true" negate="false" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                            <add input="{QUERY_STRING}" pattern="^pid=([^=&amp;]+)$" />
                        </conditions>
                        <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
                    </rule>
                    <rule name="RewriteUserFriendlyURL1" patternSyntax="ECMAScript" stopProcessing="true">
                        <match url="^([^/]+)/?$" ignoreCase="true" negate="false" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="myapp/genericmain.aspx?pid={R:1}" appendQueryString="true" redirectType="Temporary" />
                    </rule>
                </rules>
            </rewrite>
  • Re: Need help creating a rewrite rule

    Jul 30, 2009 07:10 PM|ruslany|LINK

    The rules did not work because the patterns were not correct. Make sure to place the following rules in the web.config file which is located in the myapp/ folder. Otherwise the rules will not work.

       <rewrite>
                <rules>
                    <rule name="RedirectUserFriendlyURL1" patternSyntax="ECMAScript" stopProcessing="true">
                        <match url="^genericmain\.aspx$" ignoreCase="true" negate="false" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                            <add input="{QUERY_STRING}" pattern="^pid=([^=&amp;]+)$" />
                        </conditions>
                        <action type="Redirect" url="{C:1}/genericmain.aspx" appendQueryString="false" redirectType="Permanent" />
                    </rule>
                    <rule name="RewriteUserFriendlyURL1" patternSyntax="ECMAScript" stopProcessing="true">
                        <match url="^([^/]+)/genericmain\.aspx$" ignoreCase="true" negate="false" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="genericmain.aspx?pid={R:1}" appendQueryString="true" redirectType="Temporary" />
                    </rule>
                </rules>
            </rewrite>

  • Re: Need help creating a rewrite rule

    Jul 30, 2009 07:22 PM|Rykn|LINK

    Thank you very much, that works. I just followed the wizard in IIS7, it produced the rules.

     This is working now.