Tim
Tim

Reputation: 159

IIS URL Rewrite based on root url

I'm trying to rewrite a url:

www.mydomain.com/urlfolder/filename.aspx

to point to

www.mydomain.com/urlfolder/newfile.aspx

My web config has the following:

    <rule name="PageRedirection" enabled="true" stopProcessing="true">
      <match url="urlfolder/filename.aspx" />
      <action type="Redirect" url="urlfolder/newfile.aspx" redirectType="Permanent" />
    </rule>

the problem is that this is catching urls such as:

www.mydomain.com/subdirectory/urlfolder/filename.aspx

I tried to change my url to be but the ^ didn't work. It also seems ~/ doesnt work to specify the root either.

How would I go about specifying this url from the root, w/o putting in an absolute path.

I also have:

testsite.mydomain.com/

and I want the SAME web.config deployed there to work. Thanks!

Upvotes: 2

Views: 2739

Answers (1)

jcairney
jcairney

Reputation: 3224

I know this question is nearly 4 years old, so you've probably long since moved on from needing an answer.

^ should work though:

<rule name="PageRedirection" enabled="true" stopProcessing="true">
    <match url="^urlfolder/filename.aspx" />
    <action type="Redirect" url="urlfolder/newfile.aspx" redirectType="Permanent" />
</rule>

^ specifies the beginning of the path string, so whatever you put after the ^ would match only what appears right after the site root and a slash.

e.g.

www.example.com/urlfolder/filename.aspx

would match.

www.example.com/subdirectory/urlfolder/filename.aspx

would NOT match.

The only reason I can think of why it would match /subdirectory/urlfolder/filename.aspx as you say, is that there is a copy of your web.config in /subdirectory/ in which case www.example.com/subdirectory/ is treated as the root.

If that's the case and your web.config is being copied to places other than your root directory, another option is to add the rule in %windir%\system32\inetsrv\config\ApplicationHost.config instead of a web.config. This is the file that contains all settings that apply to all of IIS on your machine, not just a particular site. In my experience, Visual Studio will refuse to open this file. However, you can edit the file in a different editor, or set the rewrite rules through the IIS GUI.

In the GUI, at the top level of the file tree, the tools available at this level include the URL Rewrite utility, and rules set there write to ApplicationHost.config.

Hope this helps

Upvotes: 3

Related Questions