首页 > 解决方案 > web.config 重写子子目录 URL

问题描述

我无法重新连接我的网站 URL,这是我的网站结构:

index.html
name-folder(folder)
name.html
   name2.html
   name3.html
     second-folder(folder)
     second.html
       second2.html
       second2.html

因为文件夹名和page.html是一样的,如果我不改文件夹名,服务器就不会渲染页面。

但是我的客户希望 URL 和以前一样,所以我尝试使用 web.config 重写 URL 规则,使第一个目录工作,但第二个子目录仍然无法工作。

例如:www.yourdomain.com/name-folder/name2 www.yourdomain.com/name/name2(<---这个作品)

对于子子目录 www.yourdomain.com/name-folder/second-folder/second (这是常规 URL) www.yourdomain.com/name-folder/second/second2 (<--当我删除第二个“ URL 上的 -folder" 名称有效,但如果我删除第一个 "-folder" 将不起作用)

www.yourdomain.com/name/second/second2 (<---that's what I need, but it's not working currently.)

我需要删除所有具有“-folder”名称的文件夹以匹配旧 URL。

这是我的 web.config 我试图删除子目录文件夹名称重写:

<!-- remove "-folder"in the child directory URL -->
<rule name="removesubfoldername" enabled="true" stopProcessing="true">
<match url="(.*)\/(.*)\/(.*)" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="\/{R:1}-folder\/{R:1}-folder\{R:2}" />
</rule>
<!--         end of re-write folder name -->

很抱歉所有的混乱,非常感谢您的帮助!谢谢!

标签: url-rewritingweb-configwindow-server

解决方案


这是有效的 web.config,它成功地从我的文件夹中删除了“-folder”名称。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
    <rule name="extensionless" stopProcessing="true">
          <match url="(.*)\.html$" />
          <action type="Redirect" url="{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="removeextension" enabled="true">
        <match url=".*" negate="false" />
    <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:0}.html" />
    </rule>
<!-- remove sub sub "-folder"in the child directory URL -->
    <rule name="removesubfoldername" enabled="true" stopProcessing="true">
    <match url="(.*)\/(.*)\/(.*)" negate="false" />
    <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{URL}" pattern="(.*)\.(.*)\.(.*)" negate="true" />
    </conditions>
      <action type="Rewrite" url="{R:1}-folder\/{R:2}-folder\/{R:3}" />
     </rule>
     <!--         end of re-write folder name -->
<!-- remove "-folder"in the child directory URL -->
    <rule name="removefoldername" enabled="true" stopProcessing="true">
    <match url="(.*)\/(.*)" negate="false" />
    <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}-folder\/{R:2}" />
    </rule>

<!--         end of re-write folder name -->
</rules>
</rewrite>
</system.webServer>
</configuration>

推荐阅读