Nginx与IIS重写规则对照转换?

location / {
          rewrite ^/(.*)$ /public/index.php?_url=/$1;
         }
location /assets/ {
        root /srv/www/Dilling/public;
 }
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
         root /srv/www/Dilling/public;
}
 location ~ /\.ht {
         deny all;
}

请问上面nginx规则如何转成iis重写规则

阅读 117
avatarAI BotBETA

在IIS中,你可以使用web.config文件来配置URL重写规则,这与Nginx的serverlocation块有些不同。下面是将你提供的Nginx规则转换为IIS重写规则的示例:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- Rule for redirecting all requests to /public/index.php -->
        <rule name="Rewrite to index.php" stopProcessing="true">
          <match url="^(.*)$" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="public/index.php?_url=/{R:1}" appendQueryString="true" />
        </rule>

        <!-- Rule for serving static assets from /assets/ -->
        <rule name="Serve assets from /assets/" stopProcessing="true">
          <match url="^assets/(.*)$" ignoreCase="false" />
          <action type="Rewrite" url="public/assets/{R:1}" appendQueryString="false" />
        </rule>

        <!-- Rule for serving static resources from specific directories -->
        <rule name="Serve static resources" stopProcessing="true">
          <match url="^(css|img|js|flv|swf|download)/(.*)$" ignoreCase="false" />
          <action type="Rewrite" url="public/{R:1}/{R:2}" appendQueryString="false" />
        </rule>

        <!-- Rule for denying access to .htaccess files -->
        <rule name="Deny access to .htaccess" stopProcessing="true">
          <match url=".*\.ht" ignoreCase="false" />
          <action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You do not have permission to view this directory or page using the credentials that you supplied." />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

这个web.config文件应该放在你IIS站点的根目录下。这里有几个要点需要注意:

  1. IIS的重写规则是基于正则表达式的,但是语法和Nginx有所不同。
  2. 在IIS中,{R:n}用于引用正则表达式中的捕获组,这与Nginx中的$1$2等类似。
  3. 在规则中使用了stopProcessing="true",这意味着如果找到匹配的规则,IIS将停止处理后续的规则。
  4. 对于静态资源的规则,appendQueryString="false"表示不将原始查询字符串附加到重写后的URL中。
  5. 对于禁止访问.htaccess文件的规则,使用了一个自定义响应,返回403 Forbidden状态码。

请注意,这些规则可能需要根据你的具体需求进行调整。在部署之前,请确保在测试环境中进行充分的测试。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏