Struggling with IIS URL Rewrite 2.0

Eric Greene
Eric Greene used Ask the Experts™
on
When it comes to the URL rewrite rules, I'm a bit of a basket case.  I need to do the following:

I have employees going to http://myapp.server.com

I need them to wind-up at http://myapp.server.com/appsubdirectory

I have tried several things, but I can't seem to get the pattern matching correct.  Help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
@ccbbc_cs - Can you please post the URL rewrite rules that you've tried?

Also, are these requests going straight to the IIS App server, or are they hitting an IIS reverse proxy first?
Eric GreeneDirector of Technology

Author

Commented:
Here are the results of the two I currently have.  Until the static IP is installed, I have to live with a temporary dynamic DNS style account (hence the two rules).

<rewrite>
            <rules>
                <rule name="WS Static IP" patternSyntax="ECMAScript">
                    <match url="^(app.mydomain.com)$" />
                    <action type="Rewrite" url="{HTTP_HOST}/application" logRewrittenUrl="true" />
                </rule>
                <rule name="WS Dynamic IP" patternSyntax="ECMAScript">
                    <match url="^(dyniphost.host.com)$" />
                    <action type="Rewrite" url="{HTTP_HOST}/WS2015" />
                </rule>
            </rules>
        </rewrite>

Open in new window

@ccbbc_cs - Thanks.

Just confirming... Do you need the URLs to be rewritten to different  directories?
{HTTP_HOST}/application
{HTTP_HOST}/WS2015

This is what the URL Rewrite snippet is indicating. However, your initial question asks to rewrite all requests going to: http://myapp.server.com to a sub directory - http://myapp.server.com/appsubdirectory.

I want to make sure that we construct the correct incoming pattern match URL and the appropriate rewrite output to solve your problem.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Eric GreeneDirector of Technology

Author

Commented:
No, I just didn't clean-up one of the directory mentions.
Eric GreeneDirector of Technology

Author

Commented:
Assume they are both /application
@ccbbc_cs - The following works on IIS 7.5, Windows 64 Bit OS... I placed the following in my web.config file for the "wwwroot" folder. All requests coming into "http://localhost" are redirected to: http://localhost/application.

This should work regardless of server IP

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>  
				<rule name="Redirect root to subdirectory" stopProcessing="true">
					<match url="^$" />
					<action type="Redirect" url="{R:0}/application" />
				</rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Open in new window

Eric GreeneDirector of Technology

Author

Commented:
Thank you Lajuan.  My problem is that other sites will need to be available as well, so catching ALL incoming URLs and redirecting them won't work for me.  I need to be able to redirect for the two specific domains  "app.mydomain.com" and "dyniphost.host.com"

Other host headers will be hosted that I won't want to redirect.
@ccbbc_cs - Okay.
So do you have three sites defined in IIS?
Site 1 - Default Site (myapp.server.com)
Site 2 - app.mydomain.com
Site 3 - dyniphost.host.com

I'm trying to determine what your users primary entry point will be. For example:
myapp.server.com - display all general content
myapp.server.com/application1 - redirect to: app.mydomain.com
myapp.server.com/application2 - redirect to: dyniphost.host.com
Eric GreeneDirector of Technology

Author

Commented:
I have one site defined with multiple host header bindings.  The site is hosted on a Windows server 2012 server.

Working with the URLS values in the rule, here is the explanation that I hope makes sense:

app.mydomain.com is for those who access the site from the inside for now.  The static IP and corresponding DNS A record are not established yet.  So, this URL only works on the inside where our internal DNS server catches it.

dyniphost.host.com is for those who are not in the building on the network.  It is a generic host name with a company that provides dynamic IPS services (because our static is not in place yet).  However, the need to access is immediate, so I need to trap this one too.

If the user types app.mydomain.com I want IIS to append /application to the end of that URL.

If the user types sunup host.host.com I want IIS to append /application to the end of that URL.

Both URLs lead to the same IIS site.
Eric GreeneDirector of Technology

Author

Commented:
I hate autocorrect.

sunup... Was supposed to by dyniphost.host.com
@ccbbc_cs - Okay, I think the following should work for you. Please note, that I also added two entries to my Windows host file for the two domain names to simulate your DNS entries. This also allowed me to test if the URL rewrite rules would work.

This is one site with multiple host header bindings...

Please see the attached screen shots and updated web.config rules below. The web.config should be in the default site "wwwroot".

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>  				
			<rule name="app.mydomain.com" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(.*)app.mydomain.com" />
                <add input="{PATH_INFO}" pattern="^/application/" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:0}/application" />
        </rule>
		<rule name="dyniphost.host.com" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(.*)dyniphost.host.com" />
                <add input="{PATH_INFO}" pattern="^/application/" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:0}/application" />
        </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Open in new window

2015-12-30_2031_My_Site_Bindings.png
2015-12-30_2030_example_host_file_ed.png
2015-12-30_2020_URL_Rewrite1.png
2015-12-30_2028_Browse_Sites.png
2015-12-30_2029_Host_Header.png
Eric GreeneDirector of Technology

Author

Commented:
Sorry it took so long to come back to this.  Lot's of circumstances in the way.
@ccbbc_cs - No problem. Glad to hear you got it resolved.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial