重写规则(Rewrite Rules)在IIS和Linux服务器的配置区别

6 篇文章 0 订阅

#开启 rewrite
RewriteEngine on
<rule enabled="true" name="myRewriteRules">

指定程序目录:设置程序目录级重写的基准URL,所有的重定向都是基于这个URL,即平台根目录(IIS不支持该指令)。
RewriteBase /

#是否不是一个目录
RewriteCond %{REQUEST_FILENAME} !-d
<add input="{REQUEST_FILENAME}" negate="true" ignoreCase="false" matchType="IsDirectory" />

#是否不是一个文件
RewriteCond %{REQUEST_FILENAME} !-f
<add input="{REQUEST_FILENAME}" negate="true" ignoreCase="false" matchType="IsFile" />

[QSA,PT,L] QSA:保留参数GET|POST传值?a=1&b=2;PT:再把这个URL交给Apache处理;L:最后一个匹配项,不再往下匹配。
^(.*)$:匹配所有的路径映射到入口文件。关于[QSA,PT,L]在IIS中的同义,参考下面的《IIS 中几个常用的 Rewrite Rules 解释》

RewriteRule ^(.*)$ index.php?s=/$1 [QSA,PT,L]
<action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />

IIS 中几个常用的 Rewrite Rules 解释:

RewriteBase: 
IIS 的 Rewrite 规则中不支持 RewriteBase 设置,若通过IIS导入 .htaccess 文件会提示如下错误:

未转换此指令,因为它不受 IIS 支持: RewriteBase /。
His directive was not converted because it is not supported by IIS: RewriteBase /.

若注销或删除 RewriteBase 行,将导入成功。
The conversion process proceeds when the said line is commented out or removed.

如果需要把程序设置在某一个子目录,需要在IIS管理器中选择这个目录,操作如下:
IIS:Win+R - inetmgr.exe - IIS - 基本设置 - 物理路径(P): - D:\iis\subdir

----------------------------------

stopProcessing: 设置 true 时,不再往下匹配后续规则,默认为 false。原文如下:
A rule may have the StopProcessing flag turned on. When the rule action is performed (i.e. the rule matched) and this flag is turned on, it means that no more subsequent rules will be processed and the request will be passed to the IIS request pipeline. By default, this flag is turned off.

----------------------------------

appendQueryString: 设置 true 时,原始URL中的查询字符串将附加到替换的URL,默认为 true。原文如下:
Specifies whether the query string from the current URL is preserved during substitution. By default, if the value of the appendQueryString flag is not specified, it is assumed to be TRUE. This means that the query string from the original URL is appended to the substituted URL.

----------------------------------

ignoreCase: 设置 true 时,模式匹配不区分大小写,默认为 true。原文如下:
Use this attribute to control whether pattern matching for the condition should be case sensitive or case insensitive.

----------------------------------

logicalGrouping: logicalGrouping 是 <Conditions> 集合的一个属性,作用如下:
logicalGrouping="MatchAll" = 匹配所有条件;
logicalGrouping="MatchAny" = 仅匹配一个条件;

条件是在重写规则的<Conditions>集合中定义的。此集合有一个名为logicalGrouping的属性,用于控制条件的计算方式。如果规则具有条件,则仅当规则模式匹配且满足以下条件时,才会执行规则操作:

Conditions are defined within a <conditions> collection of a rewrite rule. This collection has an attribute called logicalGrouping that controls how conditions are evaluated. If a rule has conditions, then the rule action is performed only if rule pattern is matched and:

All conditions were evaluated as true, provided that logicalGrouping="MatchAll" was used.
At least one of the conditions was evaluated as true, provided that logicalGrouping="MatchAny" was used.

----------------------------------

Linux下 .htaccess 配置实例:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?s=/$1 [QSA,PT,L]
</IfModule>

----------------------------------

IIS 下 web.config 配置实例:
<system.webServer>
    <rewrite>
        <rules>
            <rule name="myRewriteRules" stopProcessing="true" enabled="true">
            <match url="^(.*)$" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php?s=/{R:1}" appendQueryString="true" />
        </rule>
    </rules>
    </rewrite>
</system.webServer>

----------------------------------


The following links will help you find what you need.
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
https://social.msdn.microsoft.com/Forums/en-US/e043066b-1abd-42c4-8bf0-55398595db1f/help-converting-htaccess-to-webconfig?forum=iisurlrewritemodule
https://stackoverflow.com/questions/56647891/rewritebase-is-not-supported-in-iis
https://social.msdn.microsoft.com/Forums/en-US/4b383318-6f69-4f4c-b8f7-08309f843ba8/url-rewrite-error-when-importing-htaccess
https://stackoverflow.com/questions/44143767/htaccess-iis-mod-rewrite
https://social.msdn.microsoft.com/Forums/en-US/312adf1f-1cf8-4f07-9b19-54b591e105ba/iis7-wont-import-htaccess-file?forum=iisconfiguationandscripting
https://www.sitepoint.com/community/t/need-help-converting-htaccess-to-web-config/32092
https://processwire.com/talk/topic/8933-windows-iis/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值