JHipster:将根域重定向到www

问题描述 投票:4回答:2

我正在研究搜索引擎优化,希望将https://pomzen.com重定向到https://www.pomzen.com

是否有可能在JHipster项目中完成,还是在项目外部完成?例如在DNS记录或Tomcat的Web配置中?

java spring-boot tomcat jhipster
2个回答
1
投票

重定向必须在Web服务器级别完成。基本上,您需要Web服务器发送HTTP重定向(302或301)。 DNS无法在这里为您提供帮助。

注意:有些托管DNS平台具有解决方法(Google域,Cloudflare)。但是他们将无法处理HTTPS重定向。


0
投票

使用Tomcat的www配置将根域重定向到web.xml

  1. 创建一个项目,然后将其编译到jar库中

    tomcat-redirect
    │
    ├── src
    │   └── main
    │       └── java
    │           └── TomcatRedirect.java
    └── pom.xml
    
  2. 配置maven-compiler-plugincompile-time依赖项

    <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. 在Java代码中,实现javax.servlet.Filter并配置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. 在IDE中使用jar包目标将项目构建到Maven文件中>

    “

  5. jar文件复制到Tomcat lib文件夹

  6. 将过滤器注册和映射添加到web.xml文件夹中的Tomcat conf

  7. <!-- =========================== 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 ================================= -->
    
© www.soinside.com 2019 - 2024. All rights reserved.