Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect non www version of domain to www in Jetty

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

Both www.example.com and example.com point to my web server IP. When someone tries to open example.com he is still able to access my site that way. I want for his browser to receive HTTP 301 redirection to www.example.com instead. It is important for search rankings, as search engines must know example.com and www.example.com are one and the same.

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

How do I proceed with that? Do I need to write my own handler or is there an easier way?

like image 395
Boris Hamanov Avatar asked Aug 21 '10 21:08

Boris Hamanov


1 Answers

To avoid a cycle of redirections you have to define on what virtualhost this rule works.

<?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>
like image 189
Bauna Avatar answered Sep 19 '22 02:09

Bauna