我试图了解如何做到这一点,但似乎非常复杂(至少对于像我这样没有 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}&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/