tomcat8以上可使用如下方式 server.xml做如下修改
1 2 3 <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" />
XXXXXX请替换为自己的文件名称和密码,具体参数可查看官方描述
1 2 3 4 5 6 7 8 9 <Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true"> <SSLHostConfig> <Certificate certificateKeystoreFile="conf/XXXXXXkserver.club.pfx" certificateKeystorePassword="XXXXXX" certificateKeystoreType="PKCS12" type="RSA" /> </SSLHostConfig> </Connector>
1 <Connector port="8009" protocol="AJP/1.3" redirectPort="443" />
web.xml在最后添加如下,80端口可直接转到443
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <login-config> <!-- Authorization setting for SSL --> <auth-method>CLIENT-CERT</auth-method> <realm-name>Client Cert Users-only Area</realm-name> </login-config> <security-constraint> <!-- Authorization setting for SSL --> <web-resource-collection > <web-resource-name >SSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
使用站长工具 检查http状态
如果返回状态为302,在server.xml中的realm标签添加 transportGuaranteeRedirectStatus=”301”
301为永久重定向,302为临时重定向(搜索引擎对302不友好)
1 2 3 4 5 6 7 8 <Realm className="org.apache.catalina.realm.LockOutRealm" transportGuaranteeRedirectStatus="301" > <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm>
假如域名为xxx.com,可用此工具做转发到www.xxx.com
可配置到webapp项目中,也可直接对tomcat配置,对tomcat中的静态资源做301重定向
如果是项目中引入
1 2 3 4 5 <dependency> <groupId>org.tuckey</groupId> <artifactId>urlrewritefilter</artifactId> <version>4.0.3</version> </dependency>
tomcat中jar文件直接放到lib目录下
tomcat中的web.xml中添加如下,不用修改下面参数
1 2 3 4 5 6 7 8 9 10 <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
tomcat默认目录下添加 urlrewrite.xml 跳转规则
1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"> <urlrewrite> <rule> <name>seo redirect</name> <!--主机名不等于www.baidu.com时--> <condition name="host" operator="notequal">^www.baidu.com</condition> <from>^/(.*)</from> <!--type为permanent-redirect为永久重定向,redirect为临时重定向--> <to type="permanent-redirect" last="true">http://www.baidu.com/$1</to> </rule> </urlrewrite>