asp.net - IIS:web.config 重定向到另一个 url,保留地址栏中的原始 URL

标签 asp.net iis redirect asp-classic url-rewriting

我试图了解如何做到这一点,但似乎非常复杂(至少对于像我这样没有 IIS 经验而只有 Apache 经验的人来说)。我正在将一个网站从 Linux 移植到 Windows 服务器,在 Linux 服务器上我有一个 .htaccess 可以帮助我重写 url 以隐藏页面和参数:

RewriteRule ^store/([0-9a-zA-Z]+)$ http://www.domain.com/store/ $1/[R]

RewriteRule ^store/([0-9a-zA-Z]+)/$ http://www.domain.com/pages/store.asp?name= 1 美元

RewriteRule ^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ http://www.domain.com/store/ $1/$2/[R]

RewriteRule ^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ http://www.domain.com/pages/store.asp?name= $1&页=$2

这样当有人访问http://www.domain.com/store/client1/时正在访问http://www.domain.com/pages/store.asp?name=client1 (依此类推,最多有 4 个参数),但在浏览器地址栏中显示的 url 仍然是 http://www.domain.com/store/client1/

我找不到在 IIS 7 上执行相同操作的方法...我做了如下所示的操作:

            <rule name="Rule 5">
                <match url="^store/([0-9a-zA-Z]+)$" ignoreCase="false" />
                <action type="Redirect" url="http://www.domain.com/store/{R:1}/" redirectType="Found" />
            </rule>
            <rule name="Rule 6">
                <match url="^store/([0-9a-zA-Z]+)/$" ignoreCase="false" />
                <action type="Redirect" url="http://www.domain.com/pages/store.asp?name={R:1}" appendQueryString="false" redirectType="Found" />
            </rule>
            <rule name="Rule 7">
                <match url="^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$" ignoreCase="false" />
                <action type="Redirect" url="http://www.domain.com/store/{R:1}/{R:2}/" redirectType="Found" />
            </rule>
            <rule name="Rule 8">
                <match url="^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$" ignoreCase="false" />
                <action type="Redirect" url="http://www.domain.com/pages/store.asp?name={R:1}&amp;page={R:2}" appendQueryString="false" redirectType="Found" />
            </rule>

这适用于重定向,因此如果我调用 www.domain.com/store/arg1/arg2/,我将访问 www.domain.com/pages/store.asp?name={R:1}&page={ R:2} ,但在浏览器地址栏中我看到重定向的地址 store.asp?name={R:1}&page={R:2} 而不是原始的 www.domain.com/store/arg1/arg2/,这才是我需要的。

有办法做到这一点吗?我已经花了几个小时没有找到可行的解决方案......

最佳答案

不要使用Redirect类型的操作,而是使用Rewrite。看这个blog post了解更多信息。这会将服务器上的 URL 重写为您想要的 URL,而不是将浏览器重定向到新的 URL。

关于asp.net - IIS:web.config 重定向到另一个 url,保留地址栏中的原始 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9482222/

相关文章:

java - 无法从asp页面调用java方法:

iis - 如何在远程 IIS Web 服务器上创建 Mercurial 存储库

python - 如何在 Python 中重定向标准错误?

php - 集成 WorldPay HTML 重定向时 MC_callback 不重定向

c# - 如何从sql查询中的两个表中获取多列的唯一数据

c# - ASP.NET 中的 ClientIDMode ="AutoID"

c# - 检查 IIS 应用程序池的状态

.htaccess - 301 将所有内容重定向到新根?

c# - 从 JavaScript 调用 ASP.NET EventHandler

c# - 为什么 .NET SmtpClient 发送的电子邮件缺少 Message-Id?