匹配所有字符并重定向,除非字符串包含

发布于 2025-01-08 08:54:03 字数 542 浏览 1 评论 0原文

我正在运行几个基于模板的网站,使用 IIS 上的 web.config 将domain.com/somepage 转换为domain.com/default.asp?page=somepage 以包含来自更高级别目录的正确文件。我使用的规则如下所示:

<rule name="friendly-urls">
    <match url="^[a-zA-Z0-9_]+" />
    <conditions logicalGrouping="MatchAll" />
    <action type="Rewrite" url="default.asp?page={R:0}" />
</rule>

问题是每个网站也有一个移动版本,可以在domain.com/m/上获得,并且现在设置重定向的方式,该地址将重定向到domain.com/ default.asp?page=m 不知何故,我需要对 m/ 目录中的任何内容进行例外处理,但我对正则表达式或 web.config 的理解不够好,无法完成此操作。如果有人能提供帮助,我将非常感激。

I'm running several template based websites, using web.config on IIS to convert domain.com/somepage to domain.com/default.asp?page=somepage to include the correct files from a higher level directory. The rule I'm using looks like:

<rule name="friendly-urls">
    <match url="^[a-zA-Z0-9_]+" />
    <conditions logicalGrouping="MatchAll" />
    <action type="Rewrite" url="default.asp?page={R:0}" />
</rule>

The problem is that each website also has a mobile version, available at domain.com/m/, and the way the redirection is set up right now, that address would redirect to domain.com/default.asp?page=m Somehow I need to make an exception for anything in the m/ directory, but I don't understand regular expressions or web.config well enough to accomplish this. I would be very appreciative if anyone could help.

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

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

发布评论

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

评论(1

这样的小城市 2025-01-15 08:54:03

我不熟悉 web.config 规则,但也许这会起作用,或者至少让您开始:

<rule name="friendly-urls">
    <match url="^([a-zA-Z0-9_]+/)?([a-zA-Z0-9_]+)$" />
    <conditions logicalGrouping="MatchAll" />
    <action type="Rewrite" url="{R:1}default.asp?page={R:2}" />
</rule>

新的正则表达式将输入 URL 分为可选的第一个匹配项(其中包括正斜杠)和必需的第二个匹配项。我们想要将第一个匹配项添加到输出 URL 之前。如果没有第一个匹配项(对于非移动 URL),则应在前面添加一个空字符串。然后,第二个匹配项将照常附加到输出 URL。

给定“somepage”,它应该产生“default.asp?page=somepage”

给定“m/somepage”,它应该产生“m/default.asp?page=somepage”

我无法测试除正则表达式之外的任何内容,但我希望这有帮助!

I'm not familiar with web.config rules, but maybe this will work, or at least get you started:

<rule name="friendly-urls">
    <match url="^([a-zA-Z0-9_]+/)?([a-zA-Z0-9_]+)$" />
    <conditions logicalGrouping="MatchAll" />
    <action type="Rewrite" url="{R:1}default.asp?page={R:2}" />
</rule>

The new Regex divides the input URL into an optional first match (which includes the forward slash) and a required second match. We want to prepend the first match to the output URL. If there is no first match (for non-mobile URLs), it should prepend an empty string. The second match is then appended to the output URL as usual.

Given "somepage", it should produce "default.asp?page=somepage"

Given "m/somepage", it should produce "m/default.asp?page=somepage"

I am unable to test anything but the Regex, but I hope this helps!

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