【问题标题】:Can't figure out .htaccess to web.config redirect无法弄清楚 .htaccess 到 web.config 重定向
【发布时间】:2021-08-15 21:56:46
【问题描述】:

我不得不将 PHP 网站部署到 Windows 服务器,并且在将 .htaccess 重新写入 web.config 文件时遇到了麻烦。顺便说一句,我对 url 重写规则不是很了解。

我有 3 个 .htaccess 文件,每个目录一个。

root
 |-- app
    |-- .htaccess
 |-- public
    |-- .htaccess
 .htaccess

对于 root,我有这个规则,我相信它会将所有流量重定向到 index.php 文件所在的公共文件夹:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteRule ^$ public/ [L]
   RewriteRule (.*) public/$1 [L]
</IfModule>

对于该规则,我在 web.config 文件中尝试了下面的代码,这显然有效,并重定向到公用文件夹,因此 url 就像这样 'websitename/public/:

<rule name="Imported Rule 1" stopProcessing="true">
   <match url="^$" ignoreCase="false" />
   <action type="Rewrite" url="public/" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
   <match url="(.*)" ignoreCase="false" />
   <action type="Rewrite" url="public/{R:1}" />
</rule>

对于 App 文件夹,我有这个规则,它禁止访问该目录(我不知道迁移到 windows 服务器时是否需要它):

Options -Indexes

最后,对于 public 文件夹中的 .htaccess,我相信它会将所有流量和查询字符串重定向到 index.php 文件,因此我可以拥有类似 'website/resources/articles /name',没有 '/public' 路径:

<IfModule mod_rewrite.c>
   Options -Multiviews
   RewriteEngine On
   RewriteBase /newtemplate/public
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
 </IfModule>

我在 web.config 中尝试了这段代码,但没有成功:

<rule name="Imported Rule 1" stopProcessing="true">
   <match url="^(.+)$" ignoreCase="false" />
   <conditions>
      <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?url={R:1}" appendQueryString="true" />
</rule>

我不知道如何使用 web.config 规则来实现。

欢迎提出任何建议。

【问题讨论】:

  • 你可以直接告诉我你想把哪个url重写为另一个url,我可以写一个demo给你作为参考。关于如何使用iis url rewrite可以参考这个链接:Creating Rewrite Rules for the URL Rewrite Module.
  • 嘿,谢谢。实际上所有页面都重定向到“public”文件夹中的 index.php。所以我有类似“websitename.com/public/index.php?url=resources”的东西,其中的 url 应该变成“websitename.com/resources”。

标签: php .htaccess redirect iis web-config


【解决方案1】:

你可以试试这个规则:

<rule name="test" enabled="true" stopProcessing="true">
  <match url=".*" />
    <conditions >
      <add input="{HTTP_HOST}" pattern="^websitename.com$" negate="true" />
    </conditions>
  <action type="Redirect" url="http://websitename.com/resources" redirectType="Permanent" />
</rule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2014-05-11
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多