2016-02-15 8 views
0

我想在我的web.config重寫規則,將翻譯以下網址:的web.config麻煩重寫數字ID作爲QUERY_STRING

mypage to mypage.php 
mydirectory/mypage to mydirectory/mypage.php 
mydir2/mydir1/mypage to mydir2/mydir1/mypage.php 
mypage/12345 to mypage.php?id=12345 
mydirectory/mypage/12345 to mydirectory/mypage.php?id=12345 
mydir2/mydir1/mypage/12345 to mydir2/mydir1/mypage.php?id=12345 

作爲.htaccess文件中的Apache,我只是做與

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*)$ $1.php?%1 [L] 

我試圖在IIS中使用web.config複製此行爲。這是我到目前爲止有:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.webServer> 
<rewrite> 
    <rules> 
    <rule name="hide .php extension without losing ?id=" stopProcessing="true"> 
     <match url="^(.*)/([0-9]+)$" ignoreCase="true" /> 
     <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      <add input="{REQUEST_FILENAME}.php" matchType="IsFile" /> 
     </conditions> 
     <action type="Rewrite" url="{R:0}.php?id={R:2}" /> 
    </rule> 
    <rule name="hide .php extension" stopProcessing="true"> 
     <match url="^(.*)$" ignoreCase="true" /> 
     <conditions> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      <add input="{REQUEST_FILENAME}.php" matchType="IsFile" /> 
     </conditions> 
     <action type="Rewrite" url="{R:0}.php" /> 
    </rule> 
    </rules> 
</rewrite> 
</system.webServer> 
</configuration> 

但是,當我去我的瀏覽器,鍵入http://localhost/mydirectory/mypage/12345,我得到找不到404頁。我在404頁面上看到請求url爲http://localhost:80/mydirectory/mypage/12345,物理路徑爲C:\inetpub\wwwroot\mydirectory\mypage\12345

我在做什麼錯?

回答

0

等等,我想通了。我用

替換節點 name="hide .php extension without losing ?id="
<rule name="hide .php extension without losing ?id=" stopProcessing="true"> 
    <match url="^(.*)/([0-9]+)$" 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?id={R:2}" /> 
</rule>