【问题标题】:Translating an Apache .htaccess file to an IIS web.config将 Apache .htaccess 文件转换为 IIS web.config
【发布时间】:2009-03-31 18:52:23
【问题描述】:

我在本地使用 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 ^(.*)$ $1.do
</IfModule>

但我告知我客户的 Web 服务器是 IIS,我必须使用 web.config 文件而不是 .htaccess。谁能指导我完成这个,好吗?

【问题讨论】:

    标签: apache iis .htaccess web-config webserver


    【解决方案1】:

    这可能被视为作弊,但我们使用ISAPI_Rewrite,它让您只需将 .htaccess 文件用于 IIS。如果你能让他们把它放到服务器上,你就不需要翻译任何东西了。

    【讨论】:

      【解决方案2】:

      请注意,这仅适用于 IIS7 而不适用于 IIS6。还要安装和启用this requires FastCGI to be setup 和the URL Rewriting module。这些是您的托管人能够为您验证的内容。如果以上所有情况都成立,那么下面的文件应该可以解决问题(您可能需要调整路径,但我认为如果您向他们提供此示例文件,您的托管商将能够为您执行此操作。

      <?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>
      

      【讨论】:

      • 感谢 olle,抱歉回复晚了!我终于完成了,你的代码帮助了:D
      • 好的,如果你让我知道它有什么“问题”,我可以为未来的访客更新遮阳篷。
      【解决方案3】:

      IIS7 及以上版本可以使用URL Rewrite 模块 导入Apache .htaccess 规则。

      1. 通过Microsoft Web 平台安装程序安装URL Rewrite module
      2. 启动 IIS 管理器,然后在左侧的“连接”窗格中,选择所需的站点(例如默认网站)
      3. 在中心(功能视图)双击URL重写。
      4. 在右侧面板中单击导入规则...,然后将 .htaccess 文件中的规则粘贴到 重写规则 框中
      5. 点击右栏中的应用。

      【讨论】:

        猜你喜欢
        • 2012-01-30
        • 2017-01-05
        • 1970-01-01
        • 1970-01-01
        • 2014-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多