【问题标题】:Rewrite url in c# web.config like we have in .htaccess在 c# web.config 中重写 url,就像我们在 .htaccess 中一样
【发布时间】:2015-09-27 08:29:07
【问题描述】:

当任何人尝试访问 Web 服务器上某个文件夹中可用的 .mp4 文件时,我需要重写 URL。我只能使用 Apache 使用 .htaccess 在 php 中找到解决方案

RewriteEngine on
RewriteBase /secure
RewriteRule ^(.*)/(.*)/(.*)$ video.php?h=$1&t=$2&v=$3
RewriteRule ^$ - [F]
RewriteRule ^[^/]+\.(flv|mp4)$ - [F]

这里任何人尝试使用 url clip.baseUrl/hash/timestamp/clip.url 访问 .mp4 或 .flv 文件,url 被解析为 video.php?h=$1&t=$2&v=$3

我们可以在 c# 中使用 web.config 做这样的事情吗?我们可以禁止直接访问文件 .mp4。

【问题讨论】:

  • 你知道 IIS 吗?你可以通过 IIS 来做到这一点。
  • 我试过用谷歌搜索,但没有运气!!你能给我提供一些示例链接或一些作为答案发布的工作示例吗
  • 检查下面@Harry 的答案。

标签: c# asp.net .htaccess url-rewriting


【解决方案1】:

您可以在您的站点中编辑 web.config 文件。

例如将您的网站重定向到 HTTPS:

<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
</rule>

您需要将此规则写入 标签。 这里有一些例子:https://blog.elmah.io/web-config-redirects-with-rewrite-rules-https-www-and-more/

【讨论】:

    【解决方案2】:

    您必须编写 IHttpModule 模块类并在该类中创建 OnBeginRequest(object o, EventArgs args) 方法。在这种方法中,您可以重写 URL。但是您必须在 Web.config 中将其作为模块提及。

    【讨论】:

      【解决方案3】:

      我认为所有需要的都在这里,从在 IIS 上安装 url 重写模块到编写规则:

      http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

      【讨论】:

        猜你喜欢
        • 2016-05-23
        • 2012-06-04
        • 2014-03-06
        • 1970-01-01
        • 2018-04-04
        • 2021-03-20
        • 1970-01-01
        • 1970-01-01
        • 2020-05-24
        相关资源
        最近更新 更多