How to rewrite subdomains on the way?
Example:
- foo.bar .example.com → example.com / foo / bar
Or it would be better (reverse folders):
- foo.bar .example.com → example.com / bar / foo
The request foo.bar .example.com should send the file to / src / main / resources / static / bar / foo /index.html.
With Apache2, this is done by mod_rewrite. I found rewriting documentation with Tomcat 8 , but the question is where to put these files using spring boot?
Update
I tried using UrlRewriteFilter , but it is not possible to define the rules in the domain path by replacing regular expressions.
This is my configuration:
Maven dependency:
<dependency> <groupId>org.tuckey</groupId> <artifactId>urlrewritefilter</artifactId> <version>4.0.3</version> </dependency>
Spring Java configuration for registering a servlet filter:
@Configuration @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new UrlRewriteFilter()); registrationBean.addUrlPatterns("*"); registrationBean.addInitParameter("confReloadCheckInterval", "5"); registrationBean.addInitParameter("logLevel", "DEBUG"); return registrationBean; } }
urlrewrite.xml in / src / main / webapp / WEB -INF
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"> <urlrewrite> <rule> <name>Translate</name> <condition name="host" operator="equal">foo.bar.example.com</condition> <from>^(.*)</from> <to type="redirect">example.com/bar/foo</to> </rule> </urlrewrite>
It works with this hard-coded domain, but it should work for every subdomain like this.
java spring-boot tomcat tomcat8 embedded-tomcat-8
d0x
source share