【问题标题】:What is the alternative to rewritebase on IIS?在 IIS 上重写基础的替代方法是什么?
【发布时间】:2020-02-18 12:53:19
【问题描述】:

我的网站中有以下 .htaccess 文件 - 我必须将其重新定位到 Windows IIS 机器,并且在使用 URL-Rewrite 模块导入规则和创建 web.config 文件时遇到问题。

这是我的.htaccess 文件

# Rewrite URLs and direct to index.php

<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
##
## If moving host, change the structure below to folder name of where public folder lives
##
RewriteBase /foldername/public
##
##
##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

它在RewriteBase 上失败,但我在.htaccess 文件中理解这一点对于我的网站正常运行至关重要。

谁能给我指点?

【问题讨论】:

标签: .htaccess iis mod-rewrite


【解决方案1】:

它将转换为以下url重写规则:

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.+)$" ignoreCase="false" />
      <conditions>
        <!--##-->
        <!--## If moving host, change the structure below to folder name of where public folder lives-->
        <!--##-->
        <!--##-->
        <!--##-->
        <!--##-->
        <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="/foldername/public/index.php?url={R:1}" appendQueryString="true" />
    </rule>
  </rules>
</rewrite>

【讨论】:

  • 不应该 appendQueryString="true" (不是“false”)为了等同于问题中的 mod_rewrite 指令?第二个add 条件末尾的/= 是否出错?
  • 由于rewrite url已经添加了查询字符串,如果添加了两次就会添加多个查询字符串。
  • AFAIK,appendQueryString 将查询字符串从 原始请求 附加到替换(这里没有添加“两次”)。例如。给定/foo?bar=1 形式的请求,然后使用appendQueryString="false",您将得到/foldername/public/index.php?url=foo。但是使用appendQueryString="true"(或省略)你会得到/foldername/public/index.php?url=foo&amp;bar=1。 (?)
  • 好的,谢谢你的提及。我会修改回复。
【解决方案2】:

RewriteBase 指令为相对路径 substitutions 设置 URL 路径(覆盖 directory-prefix)。因此,您可以简单地在 RewriteRule 指令本身中硬编码 URL 路径,而不是使用 RewriteBase 指令。

例如:

RewriteRule  ^(.+)$ /foldername/public/index.php?url=$1 [QSA,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多