【问题标题】:IIS rewrite working as redirectIIS 重写作为重定向工作
【发布时间】:2013-06-23 07:28:19
【问题描述】:

我对 Url 重写和重定向的理解是(请指出我的错误假设)

  • 通过对 url 使用“重定向”选项 http://localhost/Search/VehicleDetails.aspx?id=2 改造它 进入http://localhost/Search/2/VehicleDetails.aspx,当客户端 浏览http://localhost/Search/VehicleDetails.aspx?id=2 和我们的 将规则更改重定向到 http://localhost/Search/2/VehicleDetails.aspx,服务器尝试 在/Search/2 文件夹中找到 VehicleDetails.aspx 页面。

  • 但是通过对 url 使用“重写”选项 http://localhost/Search/VehicleDetails.aspx?id=2 改造它 进入http://localhost/Search/2/VehicleDetails.aspx,当客户端 浏览http://localhost/Search/VehicleDetails.aspx?id=2,客户端 浏览器显示http://localhost/Search/2/VehicleDetails.aspx,但是 在服务器内部请求是在 VehicleDetails.aspx 页面中进行的 搜索目录,不在 /Search/2 的 VehicleDetails.aspx 中 目录...

我的问题是,我尝试按照规则重写 url

<rewrite>
      <rules>
        <rule name="Search" stopProcessing="true">
          <match url="^.*(?:Search/VehicleDetails.aspx).*$" />
          <conditions>
            <add input="{QUERY_STRING}" pattern="id=(\d+)" />
          </conditions>
          <action type="Rewrite" url="/Search/{C:1}/VehicleDetails.aspx" redirectType="Permanent" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>

它重定向到带有HTTP 404.(The resource cannot be found.)/Search/2/VehicleDetails.aspx 页面。我希望客户端浏览器显示http://localhost/Search/2/VehicleDetails.aspx,但在/Search/VehicleDetails.aspx 上发出请求..

还有我如何获得 id 的值(在本例中为 2)为 url 像

http://localhost/Search/2/VehicleDetails.aspx by Request.QueryString["id"]??

【问题讨论】:

    标签: c# asp.net redirect iis-7 url-rewriting


    【解决方案1】:

    您需要在IIS中勾选Append QueryString复选框或手动添加,例如:-

    <action type="Rewrite" url="/Search/{C:1}/VehicleDetails.aspx" 
    redirectType="Permanent" appendQueryString="true" />
    

    我注意到您已将此设置为 False,这就是 ID 没有被保留的原因,并且可能也是 404 的原因。

    【讨论】:

    • 感谢您的快速响应。我还发现 redirectType="Permanent" 选项对 type="Redirect" 有效并将其删除,但它不起作用:(..你可以还要澄清我对重写和重定向差异的查询。我的假设是否正确??
    猜你喜欢
    • 2021-10-31
    • 2013-02-27
    • 2015-01-29
    • 2013-02-27
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多