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.
Create you own Filter.
This Filter should:
chain.doFilter
Forwarding will not change any URLs in browser. Just ship content of file.
Follow code could be implementation of such filter. This is not any kind of clean code. Just quick and dirty working code:
Here is Domain class:
And you need a Controller that catches everything
I'm not sure if this is necessary to override other methods in
HttpServletRequestWrapper
. That's reason for comment.Also you have to handle cases for file delivery (not existent, ...).
Another way to solve this issue: Do it in Controller itself. This is in my opinion better way than with filter because:
/subdomain2path
.Methods
getSubdomain
andreverseDomain
are same as in answer before.This is the impl:
Domain class is same as in answer before:
You can use
Backreferences
to use grouped parts that matched in your<condition>
. Something like this -Of course, you'll have to tweak the condition rule above to stop eager matching.
More here - http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#condition
It worked for me. Hope it works for others too.
Please use the following dependency
Created urlrewrite.xml in resource folder
Added in main ApplicationRunner.java
And created a CustomURLRewriter