【问题标题】:Php: Rewrite URL using web.configPHP:使用 web.config 重写 URL
【发布时间】:2015-06-27 05:45:00
【问题描述】:

这是我的第一个 PHP 应用程序,我在这个网站上同时使用了 .html.php 页面。如果用户浏览 mysite.com/users/?abc123,它会成功地在纯 html 页面 mysite.com/users/ 上加载 id 为 'abc123' 的用户的详细信息index.html 通过 Ajax。现在,我的任务是从 URL 中删除 ?,这样如果用户浏览 mysite.com/users/abc123,那么 mysite.com/users/index.html?abc123 应该成功地提供细节。

我遵循this link 并将此规则添加到我的 web.config 中,但这似乎不起作用,我收到了:

错误:HTTP 错误 404.0 - 未找到

<rule name="Remove question mark" patternSyntax="Wildcard" stopProcessing="true">
  <match url="*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^users/(.*)$" />
  </conditions>
  <action type="Redirect" url="users/?{R:0}" redirectType="Permanent" />
</rule>

请帮助我解决以下问题:

  1. 我只能使用 web.config 进行 URL 重写(为了简单起见)
  2. 我要为其重写 URL 的页面是 .HTML 而不是 .PHP(如果重要的话)
  3. 我在 IIS 8 中本地测试我的 PHP 站点,配置为提供 PHP 服务

【问题讨论】:

标签: php html .htaccess iis url-rewriting


【解决方案1】:

这应该可以工作

<rule name="Remove question mark" stopProcessing="true">
  <match url="^/?users/([^/]+)$" ignoreCase="true" />
  <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="/users/index.html?{R:1}" />
</rule>

【讨论】:

  • 这不起作用,在浏览 localhost:8080/users/abc123 时得到相同的 HTTP 错误 404.0 - 未找到
  • 是的,它成功了,非常感谢。一件小事......它适用于 /users 和 /users/ 吗?我的意思是我需要为两者添加单独的规则吗?
  • 默认情况下它应该可以工作,因为/users/users/是相同的并且默认加载/users/index.html
猜你喜欢
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
  • 2017-08-23
相关资源
最近更新 更多