伪静态重写规则(Rewrite Rules)与真实静态文件共同存在优先级的配置

技术分享 24-02-18 阅读:6 评论:1

很多情况下我们为了优化,而在服务器上把PHP或ASPX设置为伪静态,相关的技术这里不做讨论,这里想说的是,如果访问量过高,又无条件配置负载均衡的情况下,如果存在静态文件直接访问静态文件,如果不存在,再执行伪静态设置的办法,如下:


#开启 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/


版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

分享:

扫一扫在手机阅读、分享本文

网友评论

精彩评论
  • 2024-02-18 10:37:48

    针对Win和Linus平台设置不同,可以实现电脑的访问优先级,文章尚未做到手机端的访问优先级,貌似不成功