IIS重写规则如何检查requesturl + .php文件是否存在 [英] IIS Rewrite Rule how to check requesturl + .php file is exists or not in rule

查看:79
本文介绍了IIS重写规则如何检查requesturl + .php文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查带有requesturl + .php的文件是否存在.

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

在阿帕奇,我可以使用RewriteCond%{REQUEST_FILENAME} .php -f

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

以便它可以将我的请求test.com/login重写为test.com/login.php

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

我想对iis重写规则做同样的事情.

i want to do same with iis rewrite rule.

我尝试了以下操作.

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

但它不起作用.

提前感谢您的帮助

推荐答案

在第一条规则中将{REQUEST_FILENAME}\.php更改为{REQUEST_FILENAME}.php.

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

此外,无需检查请求的URL是否作为目录存在.

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

仅当login.php退出时,要将"test.com/login"重写为"test.com/login.php",请使用:

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>

经过测试

这篇关于IIS重写规则如何检查requesturl + .php文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆