Redirecting http://www.example.com to http://example.com

How to redirect http://www.example.com to http://example.com in Tomcat? All documentation is dedicated to Apache, but I have a Java application.

+4
source share
3 answers

Apache

It must be something in the lines

# .htaccess file contents
# Apache has the same docroot as the Java web app.
# Java webapp is running on port 8084
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# For sites running on a port other than 80
RewriteCond %{HTTP_HOST}   !^domain\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*)         http://domain.com:%{SERVER_PORT}/$1 [L,R]

# And for a site running on port 80
RewriteCond %{HTTP_HOST}   !^domain\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://domain.com/$1 [L,R]
</IfModule>

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Tomcat

You can use UrlRewriteFilter in Tomcat. A rule like this should work for you in /WEB-INF/urlrewrite.xml :

<rule enabled="true">
    <name>Force HTTPS example</name>
    <note>Automatically redirects user requests.</note>
    <from>http://domain.com/(.*)$</from>
    <to type="permanent-redirect" last="true">http://www.domain.com/</to>
</rule>

Not sure I made a typo, wrote it over my head

+4
source

Tomcat8 - http://tomcat.apache.org/tomcat-8.0-doc/rewrite.html

webapp - ROOT.xml

  <Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>

rewrite.config WEB-INF -

:

  RewriteCond  %{SERVER_NAME}  ! ^www.example.com$ [NC]
  RewriteRule  ^/(.*)          http://example.com/$1  [R=301,L]

, -.

+3

, .

"hosts" ip.

:

127.0.0.1     www.example.com

"hosts" .

Windows

C:\Windows\System32\drivers\etc\hosts

Linux

/etc/hosts

. (example.com www.example.com). Hosted DNS. .

. , .

0
source

All Articles