[英]IIS7 URL Rewriting Module Replace
I really like the IIS7 URL rewriting module and so far, it worked great for me. 我非常喜欢IIS7 URL重写模块,到目前为止,它对我很有用。
There is one thing that I'm not sure how to do: I would like to permanently redirect all URLs that have encoded spaces (%20) in them to a URL that has the spaces replaced with a dash (-). 有一件事我不知道该怎么做:我想永久地将所有编码空格(%20)的URL重定向到一个用短划线( - )替换空格的URL。
So this: 所以这:
http://www.test.com/About%20Our%20Mission.aspx http://www.test.com/About%20Our%20Mission.aspx
should be redirected to this: 应该重定向到这个:
http://www.test.com/About-Our-Mission.aspx http://www.test.com/About-Our-Mission.aspx
Is that even possible with only regular expressions? 只有正则表达式才有可能吗?
There's no way to do directly what you want. 没有办法直接做你想要的。
You might settle for something like this: 你可能会满足于这样的事情:
^(.*)%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}
One of then nice things about .aspx is how easy it is to rewrite URLs with real code. 关于.aspx的好处之一是用真实代码重写URL是多么容易。 Just add a little search and replace code to your web site's Global.asax file:
只需添加一些搜索并将代码替换为您网站的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);
}
On IIS7, you have to add some entries in web.config to handle rewriting non .aspx URLs: 在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"/>
<!-- ... -->
The IIS7 URL rewriting module is great, but just because you have a hammer... IIS7 URL重写模块很棒,但仅仅因为你有锤子......
The same may be achieved in one rule with ISAPI_Rewrite 3 or Helicon Ape for any number of %20s: 对于任意数量的%20s,使用ISAPI_Rewrite 3或Helicon Ape的一个规则可以实现相同的目标:
RewriteBase /
RewriteRule ^(.*)%20(.*)$ $1-$2 [LP,R=301,L]
Perhaps I'm mad, but this seems to work... 也许我很生气,但这似乎有用......
Use a URL_Rewrite rule using Regular Expressions with this pattern: 使用具有此模式的正则表达式的URL_Rewrite规则:
^(.*) (.*)
Redirect to 重定向到
{R:1}-{R:2}
I've tested this with a single space or many spaces and it works fine for me using IIS 10. Note that it works just as well for %20
as it does for " 我用一个空格或许多空格测试了它,它对我使用IIS 10工作正常。请注意,它对于
%20
也适用于“ " in the URL string, cheers.
“在URL字符串中,欢呼。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.