web.config 将数字 ID 重写为 query_string 时遇到问题
web.config trouble with rewriting the numeric id as a query_string
我想要 web.config
中的重写规则,它将翻译以下 urls:
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
作为 Apache 中的 .htaccess
文件,我只需使用
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ .php?id=&%1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)$ .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\mypage345
。
我做错了什么?
哦等等,我明白了。我用
替换了节点 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>
我想要 web.config
中的重写规则,它将翻译以下 urls:
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
作为 Apache 中的 .htaccess
文件,我只需使用
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ .php?id=&%1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)$ .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\mypage345
。
我做错了什么?
哦等等,我明白了。我用
替换了节点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>