How to rewrite subdomains to paths?
Example:
- foo.bar .example.com --> example.com /foo/bar
Or better would be (reverse folders):
- foo.bar .example.com --> example.com /bar/foo
Requesting foo.bar .example.com should ship a file in /src/main/resources/static/ bar/foo /index.html.
With Apache2 it is done by mod_rewrite. I found documentation about rewriting with Tomcat 8 but the question is where to put this files using spring boot?
Update
I tried using the UrlRewriteFilter, but it seems not possible to define rules in the domain path using regexes substitution.
This is my configuration:
Maven Dependency:
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.3</version>
</dependency>
Spring Java Config to register the 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>
With this hardcoded domain it works, but it should work for every subdomain like this.
mr.foo.bar.example.com -> example.com/bar/foo/mr/
will fail. upvote anyway for short and easy solution :) – Orthogenetic