简体   繁体   English

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

[英]URL rewrite to index.php and remove .php extension with web.config on IIS7

I want to achieve the following - 我想实现以下目标-

  1. Redirect all request to index.php page. 将所有请求重定向到index.php页面。
  2. Want to hide .php from url but my web page should also be accessible with .php extension. 想要从网址中隐藏.php,但是我的网页也应该可以使用.php扩展名访问。
  3. Should load images, css, js files. 应该加载图片,css,js文件。
  4. In some cases I have also seen there is infinite loop. 在某些情况下,我还看到了无限循环。 How to avoid this? 如何避免这种情况?

I am using below web.config and point#1 & 3 working fine but having problem with point#2. 我在web.config和point#1&3下面使用正常,但在point#2上有问题。 It hides .php but gives 500 internal server error when I add .php in url also give 500 internal error when I add .php in form action. 它隐藏了.php,但是当我在URL中添加.php时却给出了500个内部服务器错误,当我在表单动作中添加.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>
<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM