gpt4 book ai didi

IIS 重写问题 - 捕获所有 .php 文件

转载 作者:行者123 更新时间:2023-12-04 21:39:21 24 4
gpt4 key购买 nike

在一个文件夹中,我有几个 .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+

先感谢您。

最佳答案

这个怎么样...

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

关于IIS 重写问题 - 捕获所有 .php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30779198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com