Redirecting a non-www version of a domain to www in Jetty

I cannot redirect my version of a domain without www to www with MovedContextHandler , it does not have a host to redirect.

Both www.example.com and example.com point to my web server IP address. When someone tries to open example.com , he can still access my site. I want his browser to receive an HTTP 301 redirect instead of www.example.com . This is important for search ranking, as search engines must know example.com and www.example.com are the same thing.

As a bonus, when someone tries to access example.com/somepath/somepage.html , I want to redirect HTTP 301 to www.example.com/somepath/somepage.html

How can I do it? Do I need to write my own handler or is there an easier way?

+4
source share
5 answers

I found a solution by looking at the source. You just need to specify the scheme in the url that you redirect inside the MovedContextHandler. For example: http://www.somedomain.com If you only do www.somedomain.com, the redirect will not work correctly.

This is my redirector.xml

 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure class="org.eclipse.jetty.server.handler.MovedContextHandler"> <Set name="contextPath">/</Set> <Set name="newContextURL">http://www.somedomain.com</Set> <Set name="permanent">true</Set> <Set name="discardPathInfo">false</Set> <Set name="discardQuery">false</Set> </Configure> 
0
source

To avoid a redirect loop, you must determine which virtual host this rule runs on.

 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure class="org.eclipse.jetty.server.handler.MovedContextHandler"> <Set name="contextPath">/</Set> <Set name="newContextURL">http://www.example.com</Set> <Set name="permanent">true</Set> <Set name="discardPathInfo">false</Set> <Set name="discardQuery">false</Set> <Set name="virtualHosts"> <Array type="String"> <Item>example.com</Item> </Array> </Set> </Configure> 
+6
source

You can do this using your own servlet filter:

DomainRedirectionFilter.java

 public class DomainRedirectionFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; String requestURL = httpRequest.getRequestURL().toString(); URL url = new URL(requestURL); if (!url.getHost().startsWith("www.")) { HttpServletResponse httpServletResponse = (HttpServletResponse) response; httpServletResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); httpServletResponse.setHeader("Location", requestURL.replace("://", "://www.")); } else { chain.doFilter(request, response); } } @Override public void destroy() { } } 

web.xml

 <filter> <filter-name>DomainRedirectionFilter</filter-name> <filter-class>com.invenline.orgamer.web.servletFilter.DomainRedirectionFilter</filter-class> </filter> <filter-mapping> <filter-name>DomainRedirectionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 
+1
source

I just want to write my own answer to those who use the built-in pier:

 MovedContextHandler rewriteHandler = new MovedContextHandler(); rewriteHandler.setContextPath("/"); rewriteHandler.setNewContextURL("http://www.example.com"); rewriteHandler.setPermanent(true); rewriteHandler.setDiscardPathInfo(false); rewriteHandler.setDiscardQuery(false); rewriteHandler.setVirtualHosts(new String[] {"example.com"}); 
+1
source

Two easy ways to do this:

  • If you have apache or any other front server in front of the pier, you cat use mod_rewrite or something for that front server server,
  • If you want this to be done on the side of the pier, I suggest you write a filter in your application (mapped // or regardless of your servlet mapping) that will perform the redirection task. Such a filter should not be longer than a few lines.

An IMHO filter is better than writing your own handler or configuring a berth, because you will have much less work during berth updates and product releases. You will have everything you need in your application, so you do not need to worry about env during deployment.

0
source

All Articles