JHipster: Redirect root domain to www

Viewed 232

I am working on Search Engine Optimization and I would like https://pomzen.com to be redirected to https://www.pomzen.com.

Is it possible to do it in a JHipster project, or is it done outside of the project? For example in DNS records or web config for Tomcat?

2 Answers

Redirects have to be done at the web server level. Basically you need the web server to send an HTTP Redirect (302 or 301). DNS cannot help you here.

Note : There are some hosted DNS platforms that have workarounds (Google Domains, Cloudflare). But they will not be able to handle HTTPS redirects.

Redirect root domain to www using web.xml config of Tomcat

  1. Create a project, which then be compiled to the jar library

    tomcat-redirect
    │
    ├── src
    │   └── main
    │       └── java
    │           └── TomcatRedirect.java
    └── pom.xml
    
  2. Configure maven-compiler-plugin and compile-time dependencies

    <build>
      <defaultGoal>package</defaultGoal>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>7</source>
            <target>7</target>
          </configuration>
        </plugin>
      </plugins>
    </build>
    
    <dependencies>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>provided</scope>
      </dependency>
    </dependencies>
    
  3. In Java code implement javax.servlet.Filter and configure 301 redirect

    public class TomcatRedirect implements Filter {
      @Override
      public void doFilter(ServletRequest request, ServletResponse response,
                    FilterChain chain) throws IOException, ServletException {
    
        String domainName = "localhost";
        String requestURL =
                ((HttpServletRequest) request).getRequestURL().toString();
    
        if (!requestURL.contains("www." + domainName)) {
          String newRequestURL =
                  requestURL.replace(domainName, "www." + domainName);
    
          ((HttpServletResponse) response).setStatus(301);
          ((HttpServletResponse) response).setHeader("Location", newRequestURL);
    
          System.out.println("Request: " + requestURL +
                  " was redirected to: " + newRequestURL);
        }
        chain.doFilter(request, response);
      }
    }
    
  4. Build project to the jar file using Maven package goal in your IDE

    TomcatRedirect.java

  5. Copy jar file into Tomcat lib folder

  6. Add filter registration and mapping to the Tomcat web.xml in it's conf folder

    <!-- =========================== Filter ================================= -->
    
    <filter>
        <filter-name>TomcatRedirect</filter-name>
        <filter-class>TomcatRedirect</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>TomcatRedirect</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- =========================== Filter ================================= -->
    

Works for: tomcat-9.0.33

Related