IIS Rewrite Rule how to check requesturl + .php file is exists or not in rule

时光怂恿深爱的人放手 提交于 2019-12-11 15:47:01

问题


i want to check that file with requestedurl + .php is exists or not.

in apache i can do this with RewriteCond %{REQUEST_FILENAME}.php -f

so that it can rewrite my request test.com/login to test.com/login.php

i want to do same with iis rewrite rule.

i have tried with following.

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

but it doesnt works.

thanks in advance for help


回答1:


Change {REQUEST_FILENAME}\.php to {REQUEST_FILENAME}.php in first rule.

Further there is no need to check if requested URL exists as directory.

To rewrite 'test.com/login' to 'test.com/login.php' only if login.php exits, use:

<rule name="Rewrite to PHP">
  <match url="^(.*)$" />
  <conditions>
    <add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
  </conditions>
  <action type="Rewrite" url="{R:0}.php" />
</rule>

Tested



来源:https://stackoverflow.com/questions/7595782/iis-rewrite-rule-how-to-check-requesturl-php-file-is-exists-or-not-in-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!