I had a tough time finding this over the net and finally i found a way to do it.
The problem was that as soon as http redirected to https port on ELB internally it used to get redirected back to port 80 thus creating a loop. This happens because ELB offloads the SSL and then connects to port 80 again.
Finally after some research i got the correct rewrite rule to manage X-Forwarded-Proto in such a way that even if ELB offloads the SSL tomcat gets to now that the origin request was using SSL.
This is done using Tomcat Valves on Tomcat 8. Am sure it can be done on earlier versions too. I have enabled rewrite rules on Tomcat using valves. After that it was as simple as writing redirect rules on Apache.
Here are the steps:
Step 1:
a. Open context.xml under tomcat conf folder
b. Paste the following line just below
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
Note : This will enable the valve globally. In case this needs to be enabled for specific host then it should be pasted inside the of server.xml for that particular domain
Step 2:
a. Open conf/server.xml
b. Paste the following line just above </Host>
tag
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
Step 3:
a. Open the folder where web.xml is. For example if the application is hosted under ROOT then web.xml will be under webapps/ROOT/WEB-INF
b. Similarly if the application is hosted under webapps/myappfolder then the web.xml will be under webapps/myappfolder/WEB-INF
c. In the WEB-INF folder Create a new file rewrite.config. and paste the following rewrite rule:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]