我习惯于使用 Apache 服务器,所以当启用 mod_rewrite 时,我可以创建一个 htaccess 文件并使用 URL 重写。
这是我的 htaccess 文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
现在我已经建立了这个使用这个 URL 重写模块的站点,但我了解到它是一个 Microsoft 服务器。我可以使用我的 htaccess 文件吗?有什么我需要改变才能让它工作吗?如何判断 Microsoft 服务器上是否设置了 URL 重写?
最佳答案
如果您使用 IIS 7,那么您可以使用 IIS URL 重写模块,它有一个 "Import Rules" feature可用于将 mod_rewrite 规则转换为 IIS URL 重写格式。这些特定的重写规则不会转换,因为 RewriteCond 使用“-s”和“-l”标志来检查请求的 URL 是否对应于非零大小的文件或文件系统上的符号链接(symbolic link)。如果您的应用程序不使用任何符号链接(symbolic link),那么您可以安全地将这些条件替换为:
RewriteCond %{REQUEST_FILENAME} -f [OR]
然后使用 IIS URL Rewrite UI 转换规则。这将导致这些规则:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
关于PHP、使用 htaccess 重写 URL 和 Microsoft IIS Url 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1029431/