6.x下IIS配置文件格式说明

https://www.bt.cn/bbs/thread-33097-1-1.html

web.config

<?xml version="1.0" ?>
<configuration>
	<location allowOverride="false" inheritInChildApplications="false" path=".">
		<system.webServer>
			<rewrite>
				<rules configSource="web_config\rewrite.config"/>
			</rewrite>
			<defaultDocument configSource="web_config\default.config"/>
			<httpErrors configSource="web_config\httpErrors.config"/>
			<handlers configSource="web_config\php.config"/>
		</system.webServer>
	</location>
</configuration>

web_config/default.config

<?xml version="1.0" ?>
<defaultDocument>
	<files>
		<clear/>
		<add value="index.php"/>
		<add value="index.aspx"/>
		<add value="index.asp"/>
		<add value="default.html"/>
		<add value="Default.htm"/>
		<add value="Default.asp"/>
		<add value="index.htm"/>
		<add value="index.html"/>
		<add value="iisstart.htm"/>
		<add value="default.aspx"/>
	</files>
</defaultDocument>

web_config/httpErrors.config

仅本地显示详细报错

<?xml version="1.0" ?>
<httpErrors errorMode="DetailedLocalOnly"/>

详细报错

<?xml version="1.0" encoding="UTF-8"?>
<httpErrors errorMode="Detailed" />

web_config/php.config

<?xml version="1.0" ?>
<handlers>
	<remove name="php_52"/>
	<remove name="php_53"/>
	<remove name="php_54"/>
	<remove name="php_55"/>
	<remove name="php_56"/>
	<remove name="php_70"/>
	<remove name="php_71"/>
	<remove name="php_72"/>
	<remove name="php_73"/>
	<remove name="php_74"/>
	<remove name="php_75"/>
	<add modules="FastCgiModule" name="php_55" path="*.php" requireAccess="Script" resourceType="Unspecified" scriptProcessor="C:\BtSoft\php\55\php-cgi.exe" verb="*"/>
</handlers>

php 5.6 版本

<?xml version="1.0" ?>
<handlers>
	<remove name="php_52"/>
	<remove name="php_53"/>
	<remove name="php_54"/>
	<remove name="php_55"/>
	<remove name="php_56"/>
	<remove name="php_70"/>
	<remove name="php_71"/>
	<remove name="php_72"/>
	<remove name="php_73"/>
	<remove name="php_74"/>
	<remove name="php_75"/>
	<add modules="FastCgiModule" name="php_56" path="*.php" requireAccess="Script" resourceType="Unspecified" scriptProcessor="C:\BtSoft\php\56\php-cgi.exe" verb="*"/>
</handlers>

web_config/rewrite.config (伪静态规则)

<?xml version="1.0" ?>
<rules>
	<clear/>
</rules>

以wordpress为例

<?xml version="1.0" ?>
<rules>
	<clear/>
	<rule name="category_rewrite">
		<match url="category/?(.*)"/>
		<conditions logicalGrouping="MatchAll" trackAllCaptures="false"/>
		<action appendQueryString="false" logRewrittenUrl="false" type="Rewrite" url="/index.php?category_name={R:1}"/>
	</rule>
	<rule name="tags_rewrite">
		<match url="tag/?(.*)"/>
		<conditions logicalGrouping="MatchAll" trackAllCaptures="false"/>
		<action type="Rewrite" url="index.php?tag={R:1}"/>
	</rule>
	<rule name="Main Rule_rewrite" stopProcessing="true">
		<match url=".*"/>
		<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
			<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
			<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
		</conditions>
		<action type="Rewrite" url="index.php/{R:0}"/>
	</rule>
	<rule name="wordpress_rewrite" patternSyntax="Wildcard">
		<match url="*"/>
		<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
			<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
			<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
		</conditions>
		<action type="Rewrite" url="index.php"/>
	</rule>
</rules>
  • 默认允许资源被直接访问,即不限制HTTP_REFERER为空的请求
  • 多个URL后缀与域名请使用逗号(,)隔开,如: png,jpeg,zip,js
  • 当触发防盗链时,将直接返回404状态
  • 授权域名如果多个的话,多行授权
<?xml version="1.0" ?>
<rules>
	<clear/>
	<rule name="iis_security">
		<match ignoreCase="true" url="^.*\.(jpg|jpeg|gif|png|js|css)$"/>
		<conditions>
			<add input="{HTTP_REFERER}" negate="true" pattern="://baidu.cloudduo.cn/.*"/>
			<add input="{HTTP_REFERER}" negate="true" pattern="://cloudduo.cn/.*"/>
		</conditions>
		<action type="Rewrite" url="/404.htm"/>
	</rule>
</rules>

wordpress 的伪静态 (没有使用宝塔面板)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
                <rule name="category">
                    <match url="category/?(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="/index.php?category_name={R:1}" appendQueryString="false" logRewrittenUrl="false" />
                </rule>
                <rule name="tags">
                    <match url="tag/?(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="index.php?tag={R:1}" />
                </rule>
                <rule name="Main Rule" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:0}" />
                </rule>
                <rule name="wordpress" patternSyntax="Wildcard">
                    <match url="*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule></rules>
    </rewrite>
  </system.webServer>
</configuration>

宝塔面板默认规则结合网站自带web.config 规则

<?xml version="1.0" ?>
<configuration>
	<location allowOverride="false" inheritInChildApplications="false" path=".">
		<system.webServer>
			<rewrite>
				<rules configSource="web_config\rewrite.config"/>
			</rewrite>
			<defaultDocument configSource="web_config\default.config"/>
			<httpErrors configSource="web_config\httpErrors.config"/>
			<handlers configSource="web_config\php.config"/>
		</system.webServer>
	</location>
> 	<appSettings>
>     <add key="Url" value="localhost" />
>     <add key="License" value="" />
>     <add key="DbType" value="0" />
>     <add key="SqlConnection" value="Provider=SQLOLEDB;Data Source=127.0.0.1;Initial Catalog=sq_data;User ID=sa;Password=123456" />
>     <add key="AccessConnection" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=/e/database/v3.mdb" />
>     <add key="ManageDirectory" value="/e/master/" />
>   </appSettings>
>   <system.web>
>     <httpModules>
>     </httpModules>
>     <pages validateRequest="false" enableEventValidation="false" enableViewStateMac="false" viewStateEncryptionMode="Never" />
>     <compilation defaultLanguage="C#" debug="false" />
>     <authentication mode="Forms" />
>     <customErrors mode="Off">
>       <error statusCode="404" redirect="/error404.html" />
>     </customErrors>
>     <globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" />
>     <httpRuntime maxRequestLength="1024000" executionTimeout="3600" />
>   </system.web>
</configuration>