基于根 url 的 IIS URL 重写

发布于 2024-12-14 08:07:39 字数 680 浏览 3 评论 0原文

我正在尝试重写一个网址:

www.mydomain.com/urlfolder/filename.aspx

以指向

www.mydomain.com/urlfolder/newfile.aspx

我的网络配置有以下内容:

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

问题是,这是捕获这样的网址如:

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

我试图将我的网址更改为 但 ^ 不起作用。似乎 ~/ 也无法指定根。

我将如何从根指定此 url,而不输入绝对路径。

我还有:

testsite.mydomain.com/

,我希望部署在那里的相同 web.config 能够正常工作。 谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2024-12-21 08:07:39

我知道这个问题已经问了快 4 年了,所以你可能早就不再需要答案了。

^ 应该可以工作:

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

^ 指定路径字符串的开头,因此您在 ^ 之后放置的任何内容都只会匹配紧接在 ^ 之后出现的内容站点根目录和斜杠。

例如

www.example.com/urlfolder/filename.aspx

会匹配。

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

不匹配。

我能想到为什么它会匹配 /subdirectory/urlfolder/filename.aspx 正如你所说的唯一原因是,有一个 web.config 的副本
在 /subdirectory/ 中,在这种情况下,www.example.com/subdirectory/ 被视为根目录。

如果是这种情况,并且您的 web.config 被复制到根目录以外的位置,另一个选择是在 %windir%\system32\inetsrv\config\ApplicationHost.config 中添加规则,而不是在一个 web.config。该文件包含适用于计算机上所有 IIS(而不仅仅是特定站点)的所有设置。根据我的经验,Visual Studio 将拒绝打开此文件。但是,您可以在不同的编辑器中编辑该文件,或通过 IIS GUI 设置重写规则。

在 GUI 中,文件树的顶层,此级别可用的工具包括 URL 重写实用程序,并且其中设置的规则写入 ApplicationHost.config。

希望这有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文