URL跳转时默认对搜索引擎访问使用301,以提升SEO效果

This commit is contained in:
刘祥超
2023-04-07 19:19:53 +08:00
parent 7db0c8cf62
commit 0b441021d8
3 changed files with 32 additions and 31 deletions

View File

@@ -25,6 +25,16 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
if !u.MatchRequest(this.Format) { if !u.MatchRequest(this.Format) {
continue continue
} }
var status = u.Status
if status <= 0 {
if searchEngineRegex.MatchString(this.RawReq.UserAgent()) {
status = http.StatusMovedPermanently
} else {
status = http.StatusTemporaryRedirect
}
}
if len(u.Type) == 0 || u.Type == serverconfigs.HTTPHostRedirectTypeURL { if len(u.Type) == 0 || u.Type == serverconfigs.HTTPHostRedirectTypeURL {
if u.MatchPrefix { // 匹配前缀 if u.MatchPrefix { // 匹配前缀
if strings.HasPrefix(fullURL, u.BeforeURL) { if strings.HasPrefix(fullURL, u.BeforeURL) {
@@ -38,11 +48,8 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
return false return false
} }
if u.Status <= 0 { this.processResponseHeaders(this.writer.Header(), status)
u.Status = http.StatusTemporaryRedirect http.Redirect(this.RawWriter, this.RawReq, afterURL, status)
}
this.processResponseHeaders(this.writer.Header(), u.Status)
http.Redirect(this.RawWriter, this.RawReq, afterURL, u.Status)
return true return true
} }
} else if u.MatchRegexp { // 正则匹配 } else if u.MatchRegexp { // 正则匹配
@@ -83,11 +90,8 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
} }
} }
if u.Status <= 0 { this.processResponseHeaders(this.writer.Header(), status)
u.Status = http.StatusTemporaryRedirect http.Redirect(this.RawWriter, this.RawReq, afterURL, status)
}
this.processResponseHeaders(this.writer.Header(), u.Status)
http.Redirect(this.RawWriter, this.RawReq, afterURL, u.Status)
return true return true
} else { // 精准匹配 } else { // 精准匹配
if fullURL == u.RealBeforeURL() { if fullURL == u.RealBeforeURL() {
@@ -104,11 +108,8 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
} }
} }
if u.Status <= 0 { this.processResponseHeaders(this.writer.Header(), status)
u.Status = http.StatusTemporaryRedirect http.Redirect(this.RawWriter, this.RawReq, afterURL, status)
}
this.processResponseHeaders(this.writer.Header(), u.Status)
http.Redirect(this.RawWriter, this.RawReq, afterURL, u.Status)
return true return true
} }
} }
@@ -142,10 +143,8 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
// 终止匹配 // 终止匹配
return false return false
} }
if u.Status <= 0 {
u.Status = http.StatusTemporaryRedirect this.processResponseHeaders(this.writer.Header(), status)
}
this.processResponseHeaders(this.writer.Header(), u.Status)
// 参数 // 参数
var qIndex = strings.Index(this.uri, "?") var qIndex = strings.Index(this.uri, "?")
@@ -153,7 +152,7 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
afterURL += this.uri[qIndex:] afterURL += this.uri[qIndex:]
} }
http.Redirect(this.RawWriter, this.RawReq, afterURL, u.Status) http.Redirect(this.RawWriter, this.RawReq, afterURL, status)
return true return true
} }
} else if u.Type == serverconfigs.HTTPHostRedirectTypePort { } else if u.Type == serverconfigs.HTTPHostRedirectTypePort {
@@ -200,11 +199,9 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
// 终止匹配 // 终止匹配
return false return false
} }
if u.Status <= 0 {
u.Status = http.StatusTemporaryRedirect this.processResponseHeaders(this.writer.Header(), status)
} http.Redirect(this.RawWriter, this.RawReq, afterURL, status)
this.processResponseHeaders(this.writer.Header(), u.Status)
http.Redirect(this.RawWriter, this.RawReq, afterURL, u.Status)
return true return true
} }
} }

View File

@@ -8,7 +8,7 @@ import (
) )
func (this *HTTPRequest) doRedirectToHTTPS(redirectToHTTPSConfig *serverconfigs.HTTPRedirectToHTTPSConfig) (shouldBreak bool) { func (this *HTTPRequest) doRedirectToHTTPS(redirectToHTTPSConfig *serverconfigs.HTTPRedirectToHTTPSConfig) (shouldBreak bool) {
host := this.RawReq.Host var host = this.RawReq.Host
// 检查域名是否匹配 // 检查域名是否匹配
if !redirectToHTTPSConfig.MatchDomain(host) { if !redirectToHTTPSConfig.MatchDomain(host) {
@@ -22,7 +22,7 @@ func (this *HTTPRequest) doRedirectToHTTPS(redirectToHTTPSConfig *serverconfigs.
host = redirectToHTTPSConfig.Host host = redirectToHTTPSConfig.Host
} }
} else if redirectToHTTPSConfig.Port > 0 { } else if redirectToHTTPSConfig.Port > 0 {
lastIndex := strings.LastIndex(host, ":") var lastIndex = strings.LastIndex(host, ":")
if lastIndex > 0 { if lastIndex > 0 {
host = host[:lastIndex] host = host[:lastIndex]
} }
@@ -30,18 +30,18 @@ func (this *HTTPRequest) doRedirectToHTTPS(redirectToHTTPSConfig *serverconfigs.
host = host + ":" + strconv.Itoa(redirectToHTTPSConfig.Port) host = host + ":" + strconv.Itoa(redirectToHTTPSConfig.Port)
} }
} else { } else {
lastIndex := strings.LastIndex(host, ":") var lastIndex = strings.LastIndex(host, ":")
if lastIndex > 0 { if lastIndex > 0 {
host = host[:lastIndex] host = host[:lastIndex]
} }
} }
statusCode := http.StatusMovedPermanently var statusCode = http.StatusMovedPermanently
if redirectToHTTPSConfig.Status > 0 { if redirectToHTTPSConfig.Status > 0 {
statusCode = redirectToHTTPSConfig.Status statusCode = redirectToHTTPSConfig.Status
} }
newURL := "https://" + host + this.RawReq.RequestURI var newURL = "https://" + host + this.RawReq.RequestURI
this.processResponseHeaders(this.writer.Header(), statusCode) this.processResponseHeaders(this.writer.Header(), statusCode)
http.Redirect(this.writer, this.RawReq, newURL, statusCode) http.Redirect(this.writer, this.RawReq, newURL, statusCode)

View File

@@ -15,7 +15,11 @@ import (
"sync/atomic" "sync/atomic"
) )
// 其中的每个括号里的内容都在被引用,不能轻易修改 // 搜索引擎和爬虫正则
var searchEngineRegex = regexp.MustCompile(`(?i)(60spider|adldxbot|adsbot-google|applebot|admantx|alexa|baidu|bingbot|bingpreview|facebookexternalhit|googlebot|proximic|slurp|sogou|twitterbot|yandex)`)
var spiderRegexp = regexp.MustCompile(`(?i)(python|pycurl|http-client|httpclient|apachebench|nethttp|http_request|java|perl|ruby|scrapy|php|rust)`)
// 内容范围正则,其中的每个括号里的内容都在被引用,不能轻易修改
var contentRangeRegexp = regexp.MustCompile(`^bytes (\d+)-(\d+)/(\d+|\*)`) var contentRangeRegexp = regexp.MustCompile(`^bytes (\d+)-(\d+)/(\d+|\*)`)
// 分解Range // 分解Range