PHP、使用 htaccess 重写 URL 和 Microsoft IIS Url Rewriting
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1029431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP, URL rewriting with htaccess, and Microsoft IIS Url Rewriting
提问by Andrew
I am used to working with Apache servers, so when mod_rewrite is enabled, I can create an htaccess file and use URL rewriting.
我习惯使用 Apache 服务器,所以当启用 mod_rewrite 时,我可以创建一个 htaccess 文件并使用 URL 重写。
Here's my htaccess file:
这是我的 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]
Now I've built this site that uses this URL rewriting module but I have come to learn that it is a Microsoft server. Can I use my htaccess file? Is there something I need to change to get it to work? How can I tell if URL rewriting is set up on the Microsoft server?
现在我已经建立了这个使用 URL 重写模块的站点,但我了解到它是一个 Microsoft 服务器。我可以使用我的 htaccess 文件吗?有什么我需要改变才能让它工作的吗?如何判断 Microsoft 服务器上是否设置了 URL 重写?
回答by RuslanY
If you use IIS 7 then you can use IIS URL Rewrite Module, that has an "Import Rules" featurethat can be used to translate mod_rewrite rules to IIS URL rewrite format. These particular rewrite rules will not translate because the RewriteCond uses the "-s" and "-l" flags which check if the requested URL corresponds to a non-zero size file or to a symbolic link on a file system. If your application does not use any symbolic links then you can safely replace these conditions with:
如果您使用 IIS 7,那么您可以使用 IIS URL 重写模块,该模块具有“导入规则”功能,可用于将 mod_rewrite 规则转换为 IIS URL 重写格式。这些特定的重写规则不会翻译,因为 RewriteCond 使用“-s”和“-l”标志来检查请求的 URL 是否对应于非零大小的文件或文件系统上的符号链接。如果您的应用程序不使用任何符号链接,那么您可以安全地将这些条件替换为:
RewriteCond %{REQUEST_FILENAME} -f [OR]
and then convert the rules by using IIS URL Rewrite UI. That will result in these rules:
然后使用 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>
回答by Cheeso
You can use your configuration as is, in Ionic's Isapi Rewrite Filter (IIRF).
您可以在Ionic 的 Isapi Rewrite Filter (IIRF) 中按原样使用您的配置。
IIRF is free, open-source.
IIRF 是免费的、开源的。
回答by Alan Storm
The .htaccess file is an Apache convention for providing end-user access to Apache configuration, so you're not going to be able to use it as a drop in replacement on an IIS (Microsoft) server. You wouldbe able to use it if you were running Apache on Windows.
.htaccess 文件是 Apache 约定,用于为最终用户提供对 Apache 配置的访问,因此您将无法将其用作 IIS (Microsoft) 服务器上的替代品。如果您在 Windows 上运行 Apache,您将能够使用它。
IIS7 has a URL rewriting modulethat offers support for rewriting URLs. There's also the ISAPI_Rewriteproduct which does the same for previous versions of IIS. You'll likely need some level of administrative permissions on the server to use either of these modules (i.e., no htaccess-like mechanism)
IIS7 有一个URL 重写模块,提供对重写 URL 的支持。还有ISAPI_Rewrite产品,它对以前版本的 IIS 执行相同的操作。您可能需要在服务器上具有某种级别的管理权限才能使用这些模块中的任何一个(即,没有类似 htaccess 的机制)
回答by TonyCool
You may use your above config as is in ISAPI_Rewrite 3 or Helicon Apeproducts.
您可以在 ISAPI_Rewrite 3 或Helicon Ape产品中使用上述配置。

