将http://www.example.com重定向到http://example.com [英] Redirecting http://www.example.com to http://example.com

查看:152
本文介绍了将http://www.example.com重定向到http://example.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I redirect http://www.example.com to http://example.com in Tomcat? All the documentation focuses on Apache, but I'm hosting a Java app.

推荐答案

Apache

这应该是

# .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中使用 UrlRewriteFilter .这样的规则应该在/WEB-INF/urlrewrite.xml 中为您工作:

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 if I made typo's, wrote this on top of head

这篇关于将http://www.example.com重定向到http://example.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆