web.config:隐藏文件扩展名并强制在url末尾添加尾部斜杠

时间:2017-10-28 07:44:35

标签: url redirect iis url-rewriting web-config

我无法在 webconfig 中重写和重定向规则。 我想删除文件扩展名并强制在URL末尾添加尾部斜杠我得到了一个代码,但是当我尝试使用隐藏文件扩展名的相同代码时,它可以使用文件扩展名,它需要404页面。 仅供参考 - http://example.com/about-us.php/ 正常运作

预期结果 - http://example.com/about-us/ 重定向到404页。

请告诉我隐藏文件扩展名的代码,并使用web.config文件同时添加尾部斜杠。

我已经应用了下面列出的一些规则,添加了斜杠,但是当我输入 http://example.com/about-us.php/ http://example.com/about-us >在浏览器中

仅供参考 - 它与http://example.com/about-us/一起正常运行,但我希望 http://example.com/about-us http://example.com/about-us/

这是我的规则列表:

<system.webServer>
    <httpErrors errorMode="Custom"><!-- For non-managed files -->
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" path="/404.php" responseMode="ExecuteURL" />
    </httpErrors>
<rewrite>
    <rules>

        <rule name="Add trailing slash rule 1" stopProcessing="true">  
            <match url="(.*[^/])$" />  
            <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />  
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
            </conditions>
            <action type="Redirect" redirectType="Permanent" url="{R:1}.php/" /> 
        </rule> 
        <rule name="Add trailing slash rule 2" 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/" redirectType="Permanent" />  
        </rule>
        <rule name="hide php extension" stopProcessing="true">
            <match url="^(.*)$" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                <add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
            </conditions>
            <action type="Rewrite" url="{R:1}.php/" />
        </rule>
    </rules>
</rewrite>
</system.webServer>

2 个答案:

答案 0 :(得分:1)

您需要使用以下规则:

<rules>
    <rule name="AddTrailingSlashRule" stopProcessing="true">
        <match url="(.*[^/])$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Redirect" url="{R:1}/" />
    </rule>  

    <rule name="hide php extension" stopProcessing="true">
        <match url="^(.*)/$" />
        <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        </conditions>
        <action type="Rewrite" url="{R:1}.php" />
    </rule>
</rules>

答案 1 :(得分:0)

   <rule name="Add trailing slash" stopProcessing="true">
      <match url="^(.*)([^/])$" />
      <conditions logicalGrouping="MatchAny">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
      </conditions>
      <action type="Redirect" url="{R:0}/" redirectType="Permanent" />
    </rule>

IIS用户规则:添加/到所有URL。适用于所有seo