Update http_request_host_redirect.go

This commit is contained in:
刘祥超
2023-05-27 21:01:55 +08:00
parent dc30469b2c
commit 31509392c9

View File

@@ -17,7 +17,6 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
if this.web.MergeSlashes { if this.web.MergeSlashes {
urlPath = utils.CleanPath(urlPath) urlPath = utils.CleanPath(urlPath)
} }
var fullURL = this.requestScheme() + "://" + this.ReqHost + urlPath
for _, u := range this.web.HostRedirects { for _, u := range this.web.HostRedirects {
if !u.IsOn { if !u.IsOn {
continue continue
@@ -35,10 +34,17 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
} }
} }
var fullURL = ""
if u.BeforeHasQuery() {
fullURL = this.URL()
} else {
fullURL = this.requestScheme() + "://" + this.ReqHost + urlPath
}
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) {
afterURL := u.AfterURL var afterURL = u.AfterURL
if u.KeepRequestURI { if u.KeepRequestURI {
afterURL += this.RawReq.URL.RequestURI() afterURL += this.RawReq.URL.RequestURI()
} }
@@ -104,7 +110,12 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) {
if u.KeepArgs { if u.KeepArgs {
var qIndex = strings.Index(this.uri, "?") var qIndex = strings.Index(this.uri, "?")
if qIndex >= 0 { if qIndex >= 0 {
afterURL += this.uri[qIndex:] var afterQIndex = strings.Index(u.AfterURL, "?")
if afterQIndex >= 0 {
afterURL = u.AfterURL[:afterQIndex] + this.uri[qIndex:]
} else {
afterURL += this.uri[qIndex:]
}
} }
} }