301
web.config设置301有两种情况:1、域名间的301,如:pokooo.com 301到 www.pokooo.com;2:网站内部url间的,就是动态url 301永久重定向到静态或者伪静态后的url,如:www.pokooo.com/news.asp?id=1 301到www.pokooo.com/news_1.html。下面来介绍下这两种情况如何实现。
1、域名间的301重定向
这种情况用的比较多,方法也比较简单,在web.config里的规则如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <? xml version = "1.0" encoding = "UTF-8" ?> < configuration > < system.webServer > < rewrite > < rules > < rule name = "b1y 301 Redirect" stopProcessing = "true" > < match url = ".*" /> < conditions > < add input = "{HTTP_HOST}" pattern = "^b1y.cn$" /> </ conditions > < action type = "Redirect" url = "http://www.pokooo.com{R:0}" redirectType = "Permanent" /> </ rule > </ rules > </ rewrite > </ system.webServer > </ configuration > |
2、全站进行310永久重定向,即动态url301永久重定向到静态或者伪静态后的url
在网站做伪静态或者静态化处理后,让动态的url转向到静态或者伪静态后的url,避免同一个页面有几个url出现或者用户体验的角度考虑,就需要用这种方法了。这种方式给大家说规则之前首先强调几点:
(1)网站首先要做好伪静态或者静态化处理(web.config里做伪静态处理下次分享);
(2)动态url里一般会出现&,?等符号,这些符号需要转义或者去除属性才能使用,不然你在做301的时候会不成功,”?”改为”&”,”?”去除属性用”/?”(带?问号的url跳转时asp.net会自动在后面加上问号及参数,所以遇到?时需要做去除属性处理)。
下面给大家举个例子:
把www.pokooo.com/news_show.asp\?Newsid=1 301到www.pokooo.com/news_show_1.html,这里动态url里的Newsid=1,是个变量,所以最终规则如下:
1 2 3 4 5 6 7 8 | < rule name = "news_show301" stopProcessing = "true" > < match url = ".*" /> < conditions > < add input = "{HTTP_HOST}" pattern = "^www.pokooo.com$" /> < add input = "{HTTP_URL}" pattern = "^/news_show.asp\?Newsid=(\d+)$" /> </ conditions > < action type = "Redirect" url = "https:/www.pokooo.com/news_show_{C:1}.html" appendQueryString = "false" redirectType = "Permanent" /> </ rule > |
其实第一种和第二种情况都是在一个web.config里书写的,整合后的完整规则为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <? xml version = "1.0" encoding = "UTF-8" ?> < configuration > < system.webServer > < rewrite > < rules > < rule name = "b1y 301 Redirect" stopProcessing = "true" > < match url = ".*" /> < conditions > < add input = "{HTTP_HOST}" pattern = "^b1y.cn$" /> </ conditions > < action type = "Redirect" url = "www.pokooo.com{R:0}" redirectType = "Permanent" /> </ rule > < rule name = "news_show301" stopProcessing = "true" > < match url = ".*" /> < conditions > < add input = "{HTTP_HOST}" pattern = "^www.pokooo.com$" /> < add input = "{HTTP_URL}" pattern = "^/news_show.asp\?Newsid=(\d+)$" /> </ conditions > < action type = "Redirect" url = "www.pokooo.com/news_show_{C:1}.html" appendQueryString = "false" redirectType = "Permanent" /> </ rule > </ rules > </ rewrite > </ system.webServer > </ configuration > |