将 http://www.example.com 重定向到 http://example.com

blu*_*l2k 5 tomcat tomcat7 tuckey-urlrewrite-filter

如何在 Tomcat 中将http://www.example.com重定向到http://example.com?所有文档都侧重于 Apache,但我正在托管一个 Java 应用程序。

Mar*_*gus 4

阿帕奇

这应该是这样的

# .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>
Run Code Online (Sandbox Code Playgroud)

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

雄猫

您可以在 Tomcat 中使用UrlRewriteFilter。像这样的规则应该适用于/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>
Run Code Online (Sandbox Code Playgroud)

不知道是不是写错了,就写在头上了