JHipster: Redirect root domain to www

邮差的信 提交于 2020-06-12 04:42:32

问题


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?


回答1:


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.




回答2:


To 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>compile</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 ================================= -->
    


来源:https://stackoverflow.com/questions/61753015/jhipster-redirect-root-domain-to-www

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!