首页 > 解决方案 > 如何在asp中将缩短的url重定向(或重写url)到长(原始)url

问题描述

我创建了一个功能,可以将长网址缩短为短网址。与 bit.ly 和 google url 缩短器 Fo 例如相同

它缩短了https://www.moneycontrol.com/news/india/karnataka-election-results-2018-live-updates-missing-mlas-raise-concerns-in-congress-jds-camp-2568569.html

http://hostname/snKcxi ( http://localhost:9909/snKcxi )

我创建了一个页面,即http://localhost:9909/shrtn.aspx它应该采用 url 键值:来自http://localhost/snKcxi的 snKcxi然后将控制权转移到原始长 url 即如果我点击http://localhost :9909/snKcxi它应该重定向到:https ://www.moneycontrol.com/news/india/karnataka-election-results-2018-live-updates-missing-mlas-raise-concerns-in-congress-jds-camp -2568569.html

我尝试过 URL 重写,例如:

<rewrite>
  <rules> 
    <!-- For URL redirect-->
    <rule name="redirectRule" stopProcessing="true">
      <match url="^http://localhost:9909/$" />
      <action type="Redirect" url="http://localhost:9909/shrtn.aspx" redirectType="Found"/>
    </rule>
  </rules> 
</rewrite>

但它没有做受人尊敬的功能。当我点击http://localhost/snKcxi它进入 404 错误页面!谁能帮我吗?有什么建议或改变吗??

我想做的是从短网址中获取密钥(我将根据密钥从数据库中搜索长网址)然后将其传输到长网址

标签: asp.neturl-rewriting

解决方案


在 web.config 文件中添加了以下规则并解决了问题。

 <match url="^[a-zA-Z0-9]+$" />
 <action type="Rewrite" url="/shrtn.aspx?id={R:0}"/>

推荐阅读