如何使用 URL 重写根据 IIS 中的主机名重定向到特定的登录页面?

如何使用 URL 重写根据 IIS 中的主机名重定向到特定的登录页面?

我有一个 IIS 网站,它映射(绑定)到两个单独的子域。假设 x1.domain.com 和 x2.domain.com

如何在 URL Rewrite 中创建规则,以便可以根据主机名将用户重定向到特定的登录页面?

请求 x1.domain.com 的用户应该会看到 x1-login.html 作为默认页面。请求 x2.domain.com 的用户应该会看到 x2-login.html 作为默认页面。

注意:仅当用户在浏览器中仅使用子域进行请求时才应执行此重定向。例如http://x1.domain.com或者https://x1.domain.com或者http://x2.domain.com或者https://x2.domain.com

如果请求的地址包含在路径或查询字符串中,则不应执行重定向。

问候。

答案1

要在 IIS URL Rewrite 中创建规则,以根据主机名(子域)将用户重定向到特定的登录页面,同时排除带有路径或查询字符串的请求的重定向,您可以使用以下步骤:

  1. 在您的服务器上打开 IIS 管理器。

  2. 在左侧的“连接”窗格中选择您的网站(x1.domain.com 或 x2.domain.com)。

  3. 在“功能视图”中,双击“URL Rewrite”以打开URL Rewrite模块。

  4. 点击右侧的“添加规则”来创建新规则。

  5. 选择“入站和出站规则”部分下的“空白规则”。

  6. 配置规则如下:

    • 名称:输入规则的名称(例如“子域重定向”)。
    • 匹配网址:
      • 请求的 URL:与模式匹配。
      • 使用:正则表达式。
      • 图案:^$
    • 状况:
      • 逻辑分组:全部匹配。
      • 添加以下两个条件:
        • 输入:{HTTP_HOST}
          • 模式: ^(x1|x2).domain.com$
        • 输入:{REQUEST_URI}
          • 图案:^$
    • 行动:
      • 操作类型:重定向。
      • 重定向 URL:/{C:1}-login.html
      • 重定向类型:永久(301)。
  7. 单击“应用”保存规则。

此规则会将对 x1.domain.com 和 x2.domain.com 的请求重定向至其各自的登录页面(例如 x1-login.html 和 x2-login.html),但不包含任何路径或查询字符串。仅当请求的地址在 URL 的主机名部分包含子域时,它才会执行重定向。带有路径或查询字符串的请求不会被重定向。

或者这里有一个 PowerShell 脚本,以防您需要在多台服务器上执行此操作:

# Define the website name and subdomain pattern
$websiteName = "YourWebsiteName" # Replace with your actual website name
$subdomainPattern = "^(x1|x2)\.domain\.com$"

# Define the login page names
$x1LoginPage = "x1-login.html"
$x2LoginPage = "x2-login.html"

# Load the IIS WebAdministration module
Import-Module WebAdministration

# Create the URL Rewrite rule
Add-WebConfigurationProperty -PSPath "IIS:\Sites\$websiteName" -Filter "system.webServer/rewrite/rules" -Name "." -Value @{
    name = "Subdomain Redirection"
    patternSyntax = "ECMAScript"
    stopProcessing = $true
} -AtIndex 0

Set-WebConfigurationProperty -PSPath "IIS:\Sites\$websiteName" -Filter "system.webServer/rewrite/rules/rule[@name='Subdomain Redirection']" -Name "match.url" -Value @{
    pattern = "^$"
}

Add-WebConfigurationProperty -PSPath "IIS:\Sites\$websiteName" -Filter "system.webServer/rewrite/rules/rule[@name='Subdomain Redirection']/conditions" -Name "." -Value @{
    input = "{HTTP_HOST}"
    pattern = $subdomainPattern
    negate = $false
} -AtIndex 0

Add-WebConfigurationProperty -PSPath "IIS:\Sites\$websiteName" -Filter "system.webServer/rewrite/rules/rule[@name='Subdomain Redirection']/conditions" -Name "." -Value @{
    input = "{REQUEST_URI}"
    pattern = "^$"
    negate = $false
} -AtIndex 1

Set-WebConfigurationProperty -PSPath "IIS:\Sites\$websiteName" -Filter "system.webServer/rewrite/rules/rule[@name='Subdomain Redirection']/action" -Name "type" -Value "Redirect"
Set-WebConfigurationProperty -PSPath "IIS:\Sites\$websiteName" -Filter "system.webServer/rewrite/rules/rule[@name='Subdomain Redirection']/action" -Name "url" -Value "/$($Matches[1])-$x1LoginPage"
Set-WebConfigurationProperty -PSPath "IIS:\Sites\$websiteName" -Filter "system.webServer/rewrite/rules/rule[@name='Subdomain Redirection']/action" -Name "appendQueryString" -Value $false

# Commit the changes
IISRESET

将其替换"YourWebsiteName"为您在 IIS 中网站的实际名称。请确保以提升的权限(以管理员身份运行)运行此脚本,并根据需要调整子域模式和登录页面名称。

该脚本将为您的特定场景配置 URL 重写规则。

答案2

您可以尝试在 IIS 配置中创建 URL 重写规则,根据用户访问网站时使用的主机名将用户重定向到特定的登录页面。以下是如何在 <system.webServer> 部分下为 web.config 文件编写 XML 配置的示例:

<rewrite>
  <rules>
    <!-- Rule for x1.domain.com -->
    <rule name="Redirect to x1-login.html" stopProcessing="true">
      <match url="^$" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^x1\.domain\.com$" />
      </conditions>
      <action type="Redirect" url="x1-login.html" />
    </rule>
    
    <!-- Rule for x2.domain.com -->
    <rule name="Redirect to x2-login.html" stopProcessing="true">
      <match url="^$" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^x2\.domain\.com$" />
      </conditions>
      <action type="Redirect" url="x2-login.html" />
    </rule>
  </rules>
</rewrite>
<match url="^$" />: Matches an empty URL path; it will only trigger when the user accesses the root domain without any additional path or query string.

<conditions>: Checks the HTTP_HOST: matches sub-domain or not? If so, it does the following:

<action type="Redirect" url="x1-login.html" />: Redirects user to x1-login.html or x2-login.html based on the sub-domain.

相关内容