大网数据广告活动
网站运营与故障问答

IIS URL Rewrite - Rewriting subfolders to query strings

· 52CE 我爱测

I want to redirect any traffic on my website based on the following rule: https://www.example.com/abc -> https://www.example.com/test1.aspx?c=abc https://www.example.com/def -> https://www.example.com/test1.aspx?c=def I would lik…

问题原文

原帖链接 · Ron · CC BY-SA 3.0

I want to redirect any traffic on my website based on the following rule:

  • https://www.example.com/abc -> https://www.example.com/test1.aspx?c=abc
  • https://www.example.com/def -> https://www.example.com/test1.aspx?c=def

I would like the subfolder to pass a query string, I have tried the following code but sadly had no success:

<rule name="Reditect1" stopProcessing="true">
    <match url="^(.*)test.com/(.*)" />
    <conditions>
        <add input="{R:2}" pattern="^[a-zA-Z0-9_]*$" />
    </conditions>
    <action type="Redirect" url="/test1.aspx?c={C:0}" appendQueryString="true" />
</rule>

已采纳解答

原帖链接 · Ron · CC BY-SA 3.0

So, after more research and trial and error I was able to figure it out. Here is how I have it setup now.

 <rule name="Redirect1" stopProcessing="true">
        <match url="^(.*)$" />
            <conditions>
                <add input="{R:0}" pattern="^(?!\s*$).+$"/>
                <add input="{R:0}" pattern="^[a-zA-Z0-9_]*$" />
            </conditions>
        <action type="Redirect" url="/test1.aspx?client={C:0}" appendQueryString="true" />
  </rule>

NOTE
The rule was setup at the site level and not the server level in IIS. Hence, the pattern matching ignored the domain name.

^(.*)test.com/(.*) - tried matching a test.com after the actual qualified domain name. So www.test.com/test.com/abc would satisfy the condition and not www.test.com/abc

Explanation

The rule matches any URL that comes in - Pattern (.*)

The first condition ensures that anything following the qualified domain name has at least one non-space character

The second condition ensures there are no special characters in the part that is being parsed. This was my requirement.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。