将 Apache .htaccess 文件转换为 IIS web.config
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/702526/
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
Translating an Apache .htaccess file to an IIS web.config
提问by Steph
I developed an application on my local using PHP, MySQL and Apache and it has a .htaccess file containing this:
我在本地使用 PHP、MySQL 和 Apache 开发了一个应用程序,它有一个包含以下内容的 .htaccess 文件:
#Setting the default handler.
DirectoryIndex home.do
<IfModule mod_mime.c>
#Supporting .do extensions
AddType application/x-httpd-php .do
</IfModule>
<IfModule mod_rewrite.c>
#Removing .do file extension if necessary
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.do -f
RewriteRule ^(.*)$ .do
</IfModule>
But I informed that my customer's web server is IIS and I have to use a web.config file instead of .htaccess. Can anyone direct me through this, please?
但我告知我客户的 Web 服务器是 IIS,我必须使用 web.config 文件而不是 .htaccess。任何人都可以指导我完成这个吗?
回答by ojrac
This could be seen as cheating, but we use ISAPI_Rewrite, which lets you just use the .htaccess file for IIS. If you can get them to put it on the server, you won't need to translate anything.
这可能被视为作弊,但我们使用ISAPI_Rewrite,它允许您只使用 IIS 的 .htaccess 文件。如果你能让他们把它放在服务器上,你就不需要翻译任何东西。
回答by olle
Please be aware that this will only work on IIS7 and noton IIS6. Also this requires FastCGI to be setupand the URL Rewriting moduleto be installed and enabled. These are things your hoster will be able to verify for you. If all of the above is true then the following file should do the trick ( you might need to tweak the paths but again I think your hoster will be able to do this for you if you supply them with this example file.
请注意,这仅适用于 IIS7而不适用于 IIS6。此外,这需要设置 FastCGI并安装和启用URL 重写模块。您的托管人可以为您验证这些内容。如果以上所有内容都是正确的,那么以下文件应该可以解决问题(您可能需要调整路径,但我认为如果您向他们提供此示例文件,您的托管商将能够为您执行此操作。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<sectionGroup name="system.webServer">
<sectionGroup name="rewrite">
<section name="rewriteMaps" overrideModeDefault="Allow" />
<section name="rules" overrideModeDefault="Allow" />
</sectionGroup>
</sectionGroup>
</configSections>
<system.webServer>
<!-- Mapping the .do extension to the PHP ISAPI module -->
<handlers>
<!-- the following line is very specific to your host
please check the module name and the scriptProcessor
path with the system administrator! basically this is
the same as
http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#EnableFastCGI
only in .config format. -->
<add name="MaskDoAsPHP" path=".do" verb="GET,HEAD,POST,DEBUG" modules="FastCgiModule" scriptProcessor="C:\PHP\php-cgi.exe" />
</handlers>
<!-- Setting the default handler. -->
<defaultDocument>
<files>
<clear />
<add value="home.do" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Removing do extension" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="{R1}.do" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
回答by ajcw
IIS7 and above can import Apache .htaccess rules using the URL Rewrite module.
IIS7 及更高版本可以使用URL Rewrite 模块导入 Apache .htaccess 规则。
- Install the URL Rewrite modulevia the Microsoft Web Platform Installer
- Start IIS Manager and on the left, in the Connections pane, select your required site (e.g. Default Web Site)
- In the centre (Features View) double click URL Rewrite.
- In the right panel click Import Rules...then paste your rules from the .htaccess file into the Rewrite rulesbox
- Click apply in the right column.
- 通过Microsoft Web Platform Installer安装URL Rewrite 模块
- 启动 IIS 管理器,然后在左侧的“连接”窗格中,选择您需要的站点(例如默认网站)
- 在中心(功能视图)双击URL Rewrite。
- 在右侧面板中单击“导入规则...”,然后将 .htaccess 文件中的规则粘贴到“重写规则”框中
- 单击右栏中的应用。

