IIS URL 重写和 Web.config [英] IIS URL Rewrite and Web.config

查看:33
本文介绍了IIS URL 重写和 Web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 IIS 一无所知,但我正在尝试解决将所有访问者重定向到 domain.com/page 到 domain.com/page.html 的问题

I don't understand anything about IIS, but am trying to solve this problem of redirecting all visitors to domain.com/page to domain.com/page.html

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.webServer>
    <rewrite>
          <rewriteMaps>
              <rewriteMap name="StaticRedirects">
                  <add key="/page" value="/page.html" />
              </rewriteMap>
            </rewriteMaps>
      </rewrite>
  </system.webServer>
</configuration>

出现了几个问题:

  1. 我什至不知道把文件放在哪里.有一个 User 根目录,一个 htdocs 目录,我都试过了,不高兴.
  2. 我什至不知道该帐户是否可以重写,我正在努力找出答案.

推荐答案

1) 您现有的 web.config:您已声明重写映射 .. 但尚未创建任何将使用它的规则.RewriteMap 本身完全没有任何作用.

1) Your existing web.config: you have declared rewrite map .. but have not created any rules that will use it. RewriteMap on its' own does absolutely nothing.

2) 以下是您的操作方法(它不使用重写映射——仅使用规则,这适用于少量重写/重定向):

2) Below is how you can do it (it does not utilise rewrite maps -- rules only, which is fine for small amount of rewrites/redirects):

此规则将对 /page.html 执行 SINGLE EXACT 重写(内部重定向)/page.浏览器中的 URL 将保持不变.

This rule will do SINGLE EXACT rewrite (internal redirect) /page to /page.html. URL in browser will remain unchanged.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="SpecificRewrite" stopProcessing="true">
                <match url="^page$" />
                <action type="Rewrite" url="/page.html" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

此规则 #2 将执行与上述相同的操作,但会执行 301 重定向(永久重定向),其中 URL 将在浏览器中更改.

This rule #2 will do the same as above, but will do 301 redirect (Permanent Redirect) where URL will change in browser.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="SpecificRedirect" stopProcessing="true">
                <match url="^page$" />
                <action type="Redirect" url="/page.html" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

规则#3 将尝试为任何 URL 执行此类重写,如果存在带有 .html 扩展名的文件(即对于 /page 它将检查是否 /page.html> 存在,如果存在,则发生重写):

Rule #3 will attempt to execute such rewrite for ANY URL if there are such file with .html extension (i.e. for /page it will check if /page.html exists, and if it does then rewrite occurs):

<system.webServer>
    <rewrite>
        <rules>
            <rule name="DynamicRewrite" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{REQUEST_FILENAME}.html" matchType="IsFile" />
                </conditions>
                <action type="Rewrite" url="/{R:1}.html" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

这篇关于IIS URL 重写和 Web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆