You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IIS中隐藏全站GET参数dava并实现URL路径化改造

IIS全站隐藏dava参数的URL重写配置

需求说明

服务器基于IIS+PHP+PHP Manager部署,需要实现全站范围的URL美化:将形如 http://localhost/kovusturma-sartlari/onodeme?dava=f2b80bf7 的URL,改造为 http://localhost/kovusturma-sartlari/onodeme/f2b80bf7 的路径形式,而非仅针对特定页面。

当前已配置的重写规则

<rewrite>
    <rules>
        <rule name="Add .php extension" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
            <conditions>
                <add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/home" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:1}.php" />
        </rule>
        <rule name="Rewrite to /home" 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="/home/{R:1}.php" />
        </rule>
    </rules>
</rewrite>

之前尝试的规则

<rule name="Hide Query String" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{QUERY_STRING}" pattern="dava=(.+)" />
    </conditions>
    <action type="Rewrite" url="{R:0}/{C:1}" appendQueryString="false" />
</rule>

解决方案

之前的规则仅实现了内部重写,未处理用户访问旧URL时的地址栏跳转,且规则顺序不符合IIS重写的执行逻辑。需要添加URL重定向(外部跳转)和反向内部重写两条规则,并将其放在现有规则的最前面。

完整配置代码

<rewrite>
    <rules>
        <!-- 1. 将带dava参数的URL重定向到路径形式(地址栏更新) -->
        <rule name="Redirect dava query to path" stopProcessing="true">
            <match url="^(.*)$" />
            <conditions>
                <add input="{QUERY_STRING}" pattern="^dava=([a-f0-9]+)$" />
                <!-- 匹配dava值为十六进制字符串,可根据实际值格式调整正则 -->
            </conditions>
            <action type="Redirect" url="{R:1}/{C:1}" appendQueryString="false" />
        </rule>

        <!-- 2. 将路径形式的URL内部重写回带dava参数的原地址 -->
        <rule name="Rewrite path to dava query" stopProcessing="false">
            <match url="^(.*)/([a-f0-9]+)$" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <!-- 验证对应的PHP文件存在,避免无效请求触发重写 -->
                <add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
            </conditions>
            <action type="Rewrite" url="{R:1}.php?dava={R:2}" appendQueryString="true" />
        </rule>

        <!-- 原有规则保持不变 -->
        <rule name="Add .php extension" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
            <conditions>
                <add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/home" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:1}.php" />
        </rule>
        <rule name="Rewrite to /home" 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="/home/{R:1}.php" />
        </rule>
    </rules>
</rewrite>

规则说明

  1. Redirect dava query to path:

    • 用户访问带?dava=xxx的URL时,自动跳转到路径形式的URL,地址栏同步更新,保证URL美观。
    • 正则^dava=([a-f0-9]+)$匹配示例中的十六进制dava值,若你的dava值有其他格式,可直接修改该正则表达式。
  2. Rewrite path to dava query:

    • 内部将路径末尾的dava值重写为?dava=xxx参数,确保PHP代码能通过$_GET['dava']正常获取参数,不影响原有业务逻辑。
    • 添加文件存在性验证,避免无效请求触发不必要的重写。

内容的提问来源于stack exchange,提问作者daaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 22:48:19