From 25918427bc1862af39975a44044712f70265b4ab Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 20 Jan 2018 14:54:04 +0200 Subject: [PATCH] support multi-level subdomains redirect, see previous commit for the SubdomainRedirect fast solution Former-commit-id: bc5749e46d1ae65f9d17063f3d8f2ea72510a9d8 --- _examples/README.md | 1 - _examples/subdomains/redirect/hosts | 4 ++-- _examples/subdomains/redirect/main.go | 5 +++++ core/router/path.go | 5 ++++- iris.go | 2 +- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/_examples/README.md b/_examples/README.md index b80ccda7..87fbf890 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -10,7 +10,6 @@ It doesn't always contain the "best ways" but it does cover each important featu - [Hello world!](hello-world/main.go) - [Glimpse](overview/main.go) -- [WWW](www/main.go) - [Tutorial: Online Visitors](tutorial/online-visitors/main.go) - [Tutorial: A Todo MVC Application using Iris and Vue.js](https://hackernoon.com/a-todo-mvc-application-using-iris-and-vue-js-5019ff870064) - [Tutorial: URL Shortener using BoltDB](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7) diff --git a/_examples/subdomains/redirect/hosts b/_examples/subdomains/redirect/hosts index 7437d713..c4c06478 100644 --- a/_examples/subdomains/redirect/hosts +++ b/_examples/subdomains/redirect/hosts @@ -1,4 +1,4 @@ -127.0.0.1 mydomain.com -127.0.0.1 www.mydomain.com +127.0.0.1 mydomain.com +127.0.0.1 www.mydomain.com # Windows: Drive:/Windows/system32/drivers/etc/hosts, on Linux: /etc/hosts \ No newline at end of file diff --git a/_examples/subdomains/redirect/main.go b/_examples/subdomains/redirect/main.go index 27e478fe..87ec7a66 100644 --- a/_examples/subdomains/redirect/main.go +++ b/_examples/subdomains/redirect/main.go @@ -44,6 +44,11 @@ func newApp() *iris.Application { // First argument is the 'from' and second is the 'to/target'. app.SubdomainRedirect(app, www) + // SubdomainRedirect works for multi-level subdomains as well: + // subsub := www.Subdomain("subsub") // subsub.www.mydomain.com + // subsub.Get("/", func(ctx iris.Context) { ctx.Writef("subdomain is: " + ctx.Subdomain()) }) + // app.SubdomainRedirect(subsub, www) + // // If you need to redirect any subdomain to 'www' then: // app.SubdomainRedirect(app.WildcardSubdomain(), www) // If you need to redirect from a subdomain to the root domain then: diff --git a/core/router/path.go b/core/router/path.go index 5311e4d2..51b1716f 100644 --- a/core/router/path.go +++ b/core/router/path.go @@ -129,7 +129,10 @@ func hasSubdomain(s string) bool { // if not start with "/" then it should be something else, // we don't assume anything else but subdomain. slashIdx := strings.IndexByte(s, '/') - return slashIdx > 0 || s[0] == SubdomainPrefix[0] || (len(s) >= 2 && s[0:2] == SubdomainWildcardIndicator) + return slashIdx > 0 || // for route paths + s[0] == SubdomainPrefix[0] || // for route paths + (len(s) >= 2 && s[0:2] == SubdomainWildcardIndicator) || // for party rel path or route paths + (len(s) >= 2 && slashIdx != 0 && s[len(s)-1] == '.') // for party rel, i.e www., or subsub.www. } // splitSubdomainAndPath checks if the path has subdomain and if it's diff --git a/iris.go b/iris.go index 846a2c29..40431e3c 100644 --- a/iris.go +++ b/iris.go @@ -705,7 +705,7 @@ var ErrServerClosed = http.ErrServerClosed // `Listener`, `Server`, `Addr`, `TLS`, `AutoTLS` and `Raw`. func (app *Application) Run(serve Runner, withOrWithout ...Configurator) error { // first Build because it doesn't need anything from configuration, - // this give the user the chance to modify the router inside a configurator as well. + // this gives the user the chance to modify the router inside a configurator as well. if err := app.Build(); err != nil { return errors.PrintAndReturnErrors(err, app.logger.Errorf) }