【问题标题】:IIS Rewrite problems - catching all .php filesIIS 重写问题 - 捕获所有 .php 文件
【发布时间】:2015-06-22 19:10:17
【问题描述】:

在一个文件夹中,我有几个 .php 文件(超过 10 个)

aaa.php
bbb.php
ccc.php
ddd.php
index.php

我想要实现的是正确地重写它们以便拥有友好的 URL。改写如下:

www.domain.com/data -> www.domain.com/data/index.php
www.domain.com/data/ -> www.domain.com/data/index.php
www.domain.com/data/aaa -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/ -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/chapter1 -> www.domain.com/data/aaa.php?c=chapter1

www.domain.com/data/bbb -> www.domain.com/data/bbb.php
www.domain.com/data/bbb/ -> www.domain.com/data/bbb.php
www.domain.com/data/bbb/chapter1 -> www.domain.com/data/bbb.php?c=chapter1

...

通过使用以下规则,我实现了我想要的单个 php 文件

<rule name="Friendly1" stopProcessing="true">
    <match url="^aaa$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="aaa.php" appendQueryString="false" />
</rule>
<rule name="Friendly2" stopProcessing="true">
    <match url="^aaa/$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="aaa.php" appendQueryString="false" />
</rule>
<rule name="Friendly3" stopProcessing="true">
    <match url="^aaa/(.+)\.(.+)$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.{R:2}" appendQueryString="false" />
</rule>
<rule name="Friendly4" stopProcessing="true">
    <match url="^aaa/(.+)$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="aaa.php?c={R:1}" appendQueryString="false" />
</rule>

这行得通。但是,它效率不高,而且为文件夹中的每个 php 文件复制粘贴这些规则很容易出错。

所以,我想在规则中将 aaa(.+) 交换,但在某些情况下不起作用。

<rule name="Friendly1" stopProcessing="true">
    <match url="^(.+)$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.php" appendQueryString="false" />
</rule>
<rule name="Friendly2" stopProcessing="true">
    <match url="^(.+)/$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.php" appendQueryString="false" />
</rule>
<rule name="Friendly3" stopProcessing="true">
    <match url="^(.+)/(.+)\.(.+)$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.php/{R:2}.{R:3}" appendQueryString="false" />
</rule>
<rule name="Friendly4" stopProcessing="true">
    <match url="^(.+)/(.+)$" ignoreCase="true"/>
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.php?c={R:2}" appendQueryString="false" />
</rule>

适用于以下情况:

www.domain.com/data -> www.domain.com/data/index.php
www.domain.com/data/ -> www.domain.com/data/index.php
www.domain.com/data/aaa -> www.domain.com/data/aaa.php

不适用于以下情况:

www.domain.com/data/aaa/ -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/chapter1 -> www.domain.com/data/aaa.php?c=chapter1

对于哪些情况,我会得到“未找到”。 难道我做错了什么?我该如何解决这个问题?

IIS = 7.5+

提前谢谢你。

【问题讨论】:

  • 你能列出工作案例吗?
  • @CyrilDurand 我现在添加了它们

标签: iis url-rewriting web-config rewrite


【解决方案1】:

这个怎么样...

<rewrite>
   <rules>
       <rule name="Friendly1" stopProcessing="true">
           <match url="^data/([-a-zA-Z0-9]*)/[-a-zA-Z0-9]*)$" ignoreCase="true" />
           <action type="Rewrite" url="data/{R:1}.php?c={R:2}" />
        </rule>
       <rule name="Friendly2" stopProcessing="true">
           <match url="^data/([-a-zA-Z0-9][-a-zA-Z0-9]*)$" ignoreCase="true" />
           <action type="Rewrite" url="data/{R:1}.php" />
        </rule>
        <rule name="Friendly3" stopProcessing="true">
           <match url="^data(/.*)$" ignoreCase="true" />
           <action type="Rewrite" url="data/index.php" />
        </rule>
    </rules>
</rewrite>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-08
    • 2012-06-18
    • 1970-01-01
    • 2017-06-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多