我真的很喜欢IIS7网址重写模块,到目前为止,它对我来说工作得很好。
有一件事我不确定该怎么做:我想永久地将所有编码了空格(%20)的URL重定向到一个用破折号(-)替换空格的URL。
所以这就是:
http://www.test.com/About%20Our%20Mission.aspx
应重定向至以下地址:
http://www.test.com/About-Our-Mission.aspx
仅仅使用正则表达式就可以做到吗?
发布于 2009-06-29 16:57:13
没有办法直接做你想做的事情。
你可能会满足于这样的东西:
^(.*)%20(.*)%20(.*)%20(.*) replaced by: {R:1}-{R:2}-{R:3}-{R:4}
^(.*)%20(.*)%20(.*) replaced by: {R:1}-{R:2}-{R:3}
^(.*)%20(.*) replaced by: {R:1}-{R:2}发布于 2009-07-09 15:48:12
关于.aspx的一件好事就是用真实的代码重写URL是多么容易。只需在网站的Global.asax文件中添加一些搜索和替换代码即可:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string path = HttpContext.Current.Request.Path;
// Search and replace, RegEx, etc.
HttpContext.Current.RewritePath(path);
}在IIS7上,您必须在web.config中添加一些条目来处理非.aspx URL的重写:
<system.webServer>
<handlers>
<clear/>
<add name="Brands1" path="Brands/*.html" verb="*" type="ASP.global_asax" resourceType="Unspecified"/>
<add name="Brands2" path="Brands/\?*.html" verb="*" type="ASP.global_asax" resourceType="Unspecified"/>
<!-- ... -->IIS7 URL重写模块很棒,但仅仅因为您有一个锤子……
发布于 2009-07-09 15:19:18
对于任意数量的%20,可以在ISAPI_Rewrite 3或Helicon Ape的一条规则中实现相同的结果:
RewriteBase /
RewriteRule ^(.*)%20(.*)$ $1-$2 [LP,R=301,L]https://stackoverflow.com/questions/1058789
复制相似问题