问题
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:
Create a project, which then be compiled to the
jar
librarytomcat-redirect │ ├── src │ └── main │ └── java │ └── TomcatRedirect.java └── pom.xml
Configure
maven-compiler-plugin
andcompile-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>
In Java code implement
javax.servlet.Filter
and configure301 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); } }
Build project to the
jar
file usingMaven
package goal in your IDECopy
jar
file into Tomcatlib
folderAdd filter registration and mapping to the Tomcat
web.xml
in it'sconf
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