URL重写为index.php并在IIS7上使用web.config删除.php扩展名

时间:2017-09-06 05:35:04

标签: php url-rewriting iis-7 web-config

我想实现以下目标 -

  1. 将所有请求重定向到index.php页面。
  2. 想要从网址隐藏.php,但我的网页也应该可以通过.php扩展名访问。
  3. 应加载图片,css,js文件。
  4. 在某些情况下,我也看到有无限循环。怎么避免这个?
  5. 我在 web.config 下方使用,并指出#1& 3工作正常,但有点#2的问题。它隐藏.php,但是当我在url中添加.php时,如果我在表单操作中添加.php,则会给出500内部错误。

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <defaultDocument enabled="true">
          <files>
            <clear/>
            <add value="index.php" />
            <add value="default.html" />
          </files>
        </defaultDocument>
        <security>
          <authentication>
            <basicAuthentication enabled="true" />
          </authentication>
        </security>
        <httpErrors errorMode="Custom">
          <remove statusCode="404" subStatusCode="-1" />
          <error statusCode="404" prefixLanguageFilePath="" path="/test.html" responseMode="ExecuteURL" />
        </httpErrors>
    
    
        <rewrite>
          <rules>
    
            <rule name="RewritePHP">
              <match url="(.*)" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="?path={R:1}" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    

1 个答案:

答案 0 :(得分:0)

<rewrite>
        <rules>
            <rule name="Imported Rule 1" stopProcessing="true">
                <match url="^(.*)$" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
                </conditions>
                <action type="Rewrite" url="{R:1}.php" />
            </rule>
        </rules>
    </rewrite>