Forum Moderators: phranque

Message Too Old, No Replies

Hiding the context while redirecting from Apache Web Server to Tomcat

         

kadimulam

6:32 am on May 30, 2008 (gmt 0)

10+ Year Member



Hi,

I am using mod_jk in Apache to host my website. On launching "http://www.abc.com" the home page of my application needs to be displayed and address bar should show "http://www.abc.com/index.html" but currently the address bar shows "http://www.example.com/MyApp/index.html" URL.

Can any of you please suggest a way to rewrite this URL as "http://www.example.com/index.html".

In short i want to hide the context(MyApp) in the URL. This context is name of the web application running on my Tomcat.

Please help.

Thanks
Kiran

[edited by: jdMorgan at 2:44 am (utc) on June 16, 2008]
[edit reason] example.com [/edit]

jdMorgan

3:40 pm on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once a URL is in the address bar, it's too late for Apache to do anything -- All of the rewriting/redirecting/proxying functions in Apache act on URLs received in requests from the client (browser or search robot). You must prevent links to Tomcat from ever appearing in any page content or redirect responses sent to the client.

I suspect you've got your front-end site redirecting to Tomcat. The proper way to do this is to reverse-proxy the front-end requests to Tomcat. This method won't show the Tomcat application URLs in the browsers address bar.

We tend to throw around the terms "rewrite" and "redirect" far too loosely, treating them interchangeably, and this is one of the results. Take a look at the Apache mod_proxy documentation, and also at the [P] flag for Apache mod_rewrite's RewriteRule directive, particularly the information related to reverse proxies.

BTW, the common use of reverse-proxies for this kind of application is the origin of the terms "front-end" and "back-end."

Jim

kadimulam

7:49 am on Jun 2, 2008 (gmt 0)

10+ Year Member



Hello Jim,

Thanks for the information.

I have gone through "Reverse Proxy" and "RewriteRule" in the Apache documentation. Now I need some information on the RewriteRule.Below are my settings:-

Application on Tomcat OR Context : HelloWorld
Website : www.example.com

What should be the RewriteRule if i want to Redirect the hits from "www.helloworld.com" to "HelloWorld"?

I have used the below two lines and it is working fine but showing the Context:

RewriteRule ^$ http://www.example.com/HelloWorld/jsp/home.jsp [R]
RewriteRule ^/$ http://www.example.com/HelloWorld/jsp/home.jsp [R]

What i require is if a user types "http://www.example.com" in the address bar and hits Enter, the user should be directed to "HelloWorld" Tomcat application home page by showing "http://www.example.com/jsp/home.jsp" in the address bar.

Currently the URL shown in the address bar is "http://www.example.com/HelloWorld/jsp/home.jsp" ie the Tomcat context "HelloWorld" is shown in the URL. How can i hide the context?

Please help.

Thanks,
Kiran

[edited by: jdMorgan at 2:46 am (utc) on June 16, 2008]
[edit reason] example.com [/edit]

kadimulam

7:49 am on Jun 2, 2008 (gmt 0)

10+ Year Member



Hello Jim,

Thanks for the information.

I have gone through "Reverse Proxy" and "RewriteRule" in the Apache documentation. Now I need some information on the RewriteRule.Below are my settings:-

Application on Tomcat OR Context : HelloWorld
Website : www.example..com

What should be the RewriteRule if i want to Redirect the hits from "www.example.com" to "HelloWorld"?

I have used the below two lines and it is working fine but showing the Context:

RewriteRule ^$ http://www.example.com/HelloWorld/jsp/home.jsp [R]
RewriteRule ^/$ http://www.example.com/HelloWorld/jsp/home.jsp [R]

What i require is if a user types "http://www.example.com" in the address bar and hits Enter, the user should be directed to "HelloWorld" Tomcat application home page by showing "http://www.example.com/jsp/home.jsp" in the address bar.

Currently the URL shown in the address bar is "http://www.example.com/HelloWorld/jsp/home.jsp" ie the Tomcat context "HelloWorld" is shown in the URL. How can i hide the context?

Please help.

Thanks,
Kiran

[edited by: jdMorgan at 2:47 am (utc) on June 16, 2008]
[edit reason] example.com [/edit]

jdMorgan

4:19 pm on Jun 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, you need to reverse-proxy the requests from the Apache server instance to the Tomcat instance. The code you posted above does redirects, not proxy through-puts. Redirects always 'expose' the target URL to the client, since this must be done for a client redirect to work. Proxy throughputs simply pass the request from the Apache server to another server, in your case, Tomcat, without informing or involving the client.

I'd like to help more, but I know nothing of Tomcat. See the [P] and [PT] flags for mod_rewrite's RewriteRule directive, and re-read the Apache mod_proxy module documentation and tutorials.

Jim

g1smd

4:39 pm on Jun 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Additionally, you shouldn't need to show the index file filename to your users at all.

The shorter slash-ended URL will suffice.

kadimulam

9:31 am on Jun 3, 2008 (gmt 0)

10+ Year Member



Thanks Joe for the reply.

I tried the following. Please refer the "RewriteRule" section and "ProxyPass" section below.

----------------------------

<VirtualHost *:80>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\"" special
ServerAdmin webmaster@example.com
ServerName example.com
DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
#ServerAlias server
ErrorLog logs/error11.log
CustomLog logs/11.log special

RewriteEngine On
RewriteRule ^/(.*.(asp[x]?¦exe¦cmd¦bat¦dll¦com¦cgi¦p(hp[3-6]?¦html¦l¦y¦erl))$¦cgi-bin) - [F]

RewriteRule ^$ /HelloWorld [R]
RewriteRule ^/$ /HelloWorld [R]

ProxyRequests Off
ProxyPass /HelloWorld http://example.com/jsp/home.jsp
#ProxyPassReverse /HelloWorld/ http://example.com/jsp/home.jsp

RewriteLog "logs/helloworld_rewrite.log"
RewriteLogLevel 1

# Deny direct access to WEB-INF
<LocationMatch ".*WEB-INF.*">
AllowOverride None
deny from all
</LocationMatch>

JkMount /*.jsp wrkr
JkMount /servlet/* wrkr
JkMount /* wrkr
</VirtualHost>

----------------------------

The above method shows "http://example.com/HelloWorld" in URL and displays a 404 error page(HTTP Status 404 - /jsp/home.jsp).

The above configuration didnt hide the context "HelloWorld".

Please help.

Thanks,
Kiran

[edited by: jdMorgan at 2:49 am (utc) on June 16, 2008]
[edit reason] example.com [/edit]

kadimulam

9:37 am on Jun 3, 2008 (gmt 0)

10+ Year Member



Hi Jim,

I refered the following link for the above configuration:

[apachetutor.org...]

Thanks,
Kiran

jdMorgan

5:37 pm on Jun 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace both RewriteRules with:

RewriteRule ^/?$ /HelloWorld [b][L][/b]

You cannot use [R] -- This results in an external redirect, which *will* updates the browser address bar.

Jim

jdMorgan

8:39 pm on Jun 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... and if that still doesn't work, you may need to use

RewriteRule ^/?$ /HelloWorld [PT,L]

Jim

kadimulam

3:10 am on Jun 4, 2008 (gmt 0)

10+ Year Member



Hi Jim,

The first didnt worked and second worked, but it displayed "HelloWorld" in the address bar. And when i tried to do Reverse Proxy with the following, it gave me the same error(HTTP Status 404 - /jsp/home.jsp).

ProxyRequests Off
ProxyPass /HelloWorld [helloworld.com...]
#ProxyPassReverse /HelloWorld/ [helloworld.com...]

Please help.

Thanks,
Kiran

kadimulam

5:11 am on Jun 11, 2008 (gmt 0)

10+ Year Member



Hi Jim,

Any information on this?

Thanks,
Kiran

jdMorgan

2:43 am on Jun 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got a trailing slash on your ProxyPassReverse directive, but none on your ProxyPass -- Any specific reason why those are different?

If a simple internal rewrite as I posted above seems to change the client address bar, then that indicates that a subsequent RewriteRule (mod_rewrite), Redirect or RedirectMatch directive (mod_alias), or a script is forcing an external redirect, and thereby 'exposing' the previously-completed internal rewrite to the client. You'll have to find and fix that mechanism.

Jim