How to redirect from HTTP to HTTPS on Tomcat Server


On this page, we will learn how to redirect an application from HTTP to HTTPS on Tomcat Server automatically. But before directly jumping to the configuration, make sure you have installed and configured the self-signed SSL on the Tomcat server.

And if SSL is not configured yet, you can take help from our previous blog on How to configure SSL/HTTPS on Tomcat Server.

How to redirect from HTTP to HTTPS on Tomcat Server
Follow the below steps to configure and redirect from HTTP to HTTPS:

Step 1: Open the server.xml file from ~{Tomcat-Installation-Directory}/conf directory and set the redirect port to the HTTPS connector port for the HTTP connector.

server.xml
<Connector 
	port="8090" 
	protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" 
/>

Step 2: Add the below configuration in ~{Tomcat-Installation-Directory}/conf/web.xml file but make sure to add it after all the Servlet mapping tags.

web.xml
<security-constraint>
	<web-resource-collection>
		<web-resource-name>All Web Application</web-resource-name>
		<url-pattern>/*</url-pattern>
	</web-resource-collection>
	<user-data-constraint>
		<transport-guarantee>CONFIDENTIAL</transport-guarantee>
	</user-data-constraint>
</security-constraint>

Step 3: Now restart the Tomcat server and hit http://localhost:8090/docs/ on your browser and it will automatically redirected to https://localhost:8443/docs/.

References

  1. How to configure SSL/HTTPS on Tomcat Server
  2. Website- How to redirect non www to www in htaccess

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.