Fix from to www nginx annotation

This commit is contained in:
Michael 2026-02-26 09:56:04 +01:00 committed by GitHub
parent 1ee0c8b4f0
commit fc65ec8839
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 9 deletions

View File

@ -138,6 +138,51 @@ func TestRedirectRegexHandler(t *testing.T) {
expectedURL: "https://foo",
expectedStatus: http.StatusTemporaryRedirect,
},
{
desc: "www-redirect without port",
config: dynamic.RedirectRegex{
Regex: `(https?)://[^/:]+(:[0-9]+)?/(.*)`,
Replacement: "$1://example.com$2/$3",
Permanent: true,
},
url: "http://www.example.com/path",
expectedURL: "http://example.com/path",
expectedStatus: http.StatusMovedPermanently,
},
{
desc: "www-redirect with port",
config: dynamic.RedirectRegex{
Regex: `(https?)://[^/:]+(:[0-9]+)?/(.*)`,
Replacement: "$1://example.com$2/$3",
Permanent: true,
},
url: "http://www.example.com:8080/path",
expectedURL: "http://example.com:8080/path",
expectedStatus: http.StatusMovedPermanently,
},
{
desc: "www-redirect without port, root path",
config: dynamic.RedirectRegex{
Regex: `(https?)://[^/:]+(:[0-9]+)?/(.*)`,
Replacement: "$1://example.com$2/$3",
Permanent: true,
},
url: "http://www.example.com/",
expectedURL: "http://example.com/",
expectedStatus: http.StatusMovedPermanently,
},
{
desc: "www-redirect HTTPS without port",
config: dynamic.RedirectRegex{
Regex: `(https?)://[^/:]+(:[0-9]+)?/(.*)`,
Replacement: "$1://example.com$2/$3",
Permanent: true,
},
secured: true,
url: "https://www.example.com/path",
expectedURL: "https://example.com/path",
expectedStatus: http.StatusMovedPermanently,
},
{
desc: "HTTP to HTTP POST permanent",
config: dynamic.RedirectRegex{

View File

@ -1164,9 +1164,9 @@ func applyFromToWwwRedirect(hosts map[string]bool, ruleHost, routerName string,
fromToWwwRedirectMiddlewareName := routerName + "-from-to-www-redirect"
conf.HTTP.Middlewares[fromToWwwRedirectMiddlewareName] = &dynamic.Middleware{
RedirectRegex: &dynamic.RedirectRegex{
Regex: `(https?)://[^/]+:([0-9]+)/(.*)`,
Replacement: fmt.Sprintf("$1://%s:$2/$3", ruleHost),
Permanent: true,
Regex: `(https?)://[^/:]+(:[0-9]+)?/(.*)`,
Replacement: fmt.Sprintf("$1://%s$2/$3", ruleHost),
StatusCode: ptr.To(http.StatusPermanentRedirect),
},
}

View File

@ -1238,9 +1238,9 @@ func TestLoadIngresses(t *testing.T) {
Middlewares: map[string]*dynamic.Middleware{
"default-ingress-with-www-host-rule-0-path-0-from-to-www-redirect": {
RedirectRegex: &dynamic.RedirectRegex{
Regex: `(https?)://[^/]+:([0-9]+)/(.*)`,
Replacement: "$1://www.host.localhost:$2/$3",
Permanent: true,
Regex: `(https?)://[^/:]+(:[0-9]+)?/(.*)`,
Replacement: "$1://www.host.localhost$2/$3",
StatusCode: ptr.To(http.StatusPermanentRedirect),
},
},
"default-ingress-with-www-host-rule-0-path-0-retry": {
@ -1312,9 +1312,9 @@ func TestLoadIngresses(t *testing.T) {
Middlewares: map[string]*dynamic.Middleware{
"default-ingress-with-host-rule-0-path-0-from-to-www-redirect": {
RedirectRegex: &dynamic.RedirectRegex{
Regex: `(https?)://[^/]+:([0-9]+)/(.*)`,
Replacement: "$1://host.localhost:$2/$3",
Permanent: true,
Regex: `(https?)://[^/:]+(:[0-9]+)?/(.*)`,
Replacement: "$1://host.localhost$2/$3",
StatusCode: ptr.To(http.StatusPermanentRedirect),
},
},
"default-ingress-with-host-rule-0-path-0-retry": {