2016-08-19 67 views
2

我不是很熟悉IIS,但我試圖隱藏.php擴展請求,我也希望它在添加尾部斜槓時工作。IIS-服務無擴展名的PHP文件,有或沒有斜線

以下工作很好地爲PHP文件提供不帶.php擴展名的文件,但它不適用於結尾的斜槓(404)。

<rule name="rewrite php"> 
    <match url="(.*)" /> 
    <conditions logicalGrouping="MatchAll"> 
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
    <add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" /> 
    </conditions> 
    <action type="Rewrite" url="{R:1}.php" /> 
</rule> 

什麼是正確的方法來允許這與無斜線或無斜線工作?

回答

1

對我來說,解決辦法是如下:

<rule name="Add Trailing Slash" stopProcessing="true"> 
    <match url=".*[^/]$" /> 
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
    <add input="{REQUEST_FILENAME}" pattern=".+?\.\w+$" negate="true" /> 
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="{ToLower:{R:0}}/" /> 
</rule> 

<rule name="rewrite php"> 
    <match url="(.*)(/)" /> 
    <conditions logicalGrouping="MatchAll"> 
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
    <add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" /> 
    </conditions> 
    <action type="Rewrite" url="{R:1}.php" /> 
</rule> 

第一部分部隊尾隨斜線所有非目錄和非直接文件的請求。請注意第二塊中的<match url="(.*)(/)" />。這將尾部斜線分隔爲單獨的正則表達式匹配組。這樣,當文件被請求 PHP擴展(<action type="Rewrite" url="{R:1}.php" />)時,從URI中刪除尾部斜線。

+0

我不得不刪除這行''。我該如何將.php文件重定向到最終的url路徑? – onebitrocket

+0

@onebitrocket,在我的情況下,最終的url路徑是.php文件名,除了沒有.php擴展名。你想做什麼? 以下內容:''只是告訴'重寫php'塊直接忽略請求到'.php'文件。 –