Solved

Apache_Tomcat url and domain redirection

Posted on 2011-09-11
11
291 Views
Last Modified: 2012-05-12
Hi Guys,

I'm using Apache as a proxy and Tomcat as a application server.

I'm looking for below redirection :

1) . When we hit alpha.domain.com/app1 it should be redirected to somedomain.com
      And before hitting somedomain.com it's req url (x-forwarded-host) should be  
      changed  to  beta.domain.com.

2) And all the url's after alpha.domain.com/app1/page1.jsp should be redirected to beta.domain.com/app1/page1.jsp.

Below is my httpd.conf file :

<VirtualHost system01:80>

ServerName domain.com
ServerAlias alpha.domain.com
ErrorLog   /var/log/httpd/domain.error.log
CustomLog  /var/log/httpd/domain.log combined

SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1

ProxyRequests Off
ProxyPreserveHost On

Options +FollowSymlinks
RewriteEngine On
RewriteLog /var/log/httpd/domain_rewrite.log
RewriteLogLevel 9

rewriteCond %{HTTP_HOST} alpha.domain.com
RewriteCond %{REQUEST_URI} !^/alpha.domain.com
rewriteRule ^(.*) http://beta.domain.com%{REQUEST_URI}

   <Proxy *>
     AddDefaultCharset Off
     Order deny,allow
#     Allow from all
     Allow from 10.120.69.158
   </Proxy>

   ProxyPass / http://system01:8083/
   ProxyPassReverse / http://system01:8083/
</VirtualHost>


Using the above config it's not working , as desired.


Please suggest me how to fix this.

0
Comment
Question by:rameshsukhi123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 

Author Comment

by:rameshsukhi123
ID: 36521002
can anybody please reply ... i need help urgently
0
 
LVL 4

Expert Comment

by:Ryan Weaver
ID: 36522111
It seems like the conf is doing some proxying, while you are trying to redirect the urls.

You may not be able to redirect and proxy in the same virtual host space.
0
 

Author Comment

by:rameshsukhi123
ID: 36522632
Thanks for the reply..

I've configured a separate virtual host for this and below is the config


<VirtualHost system02:80>
   ServerName alpha.domain.com

   ErrorLog /var/log/httpd/alpha.error.log
   CustomLog /var/log/httpd/alpha.log combined

   SetEnv force-proxy-request-1.0 1
   SetEnv proxy-nokeepalive 1

   ProxyRequests Off
   ProxyPreserveHost On

Options +FollowSymlinks
RewriteEngine On
RewriteLog /var/log/httpd/alphadomain_rewrite.log
RewriteLogLevel 9

RewriteCond %{HTTP_HOST} alpha.domain.com
RewriteCond %{REQUEST_URI} !^/alpha.domain.com
RewriteRule ^/app1/^(.*) http://beta.domain.com/$1 [QSA]

RewriteRule ^/app1/$       %1      [NC,L,R=301]
RewriteRule ^/app1/$       http://beta.domain.com/%1      [NC,L,R=301]
</VirtualHost>


      When we hit alpha.domain.com/app1 it should be redirected to somedomain.com
      And before hitting somedomain.com it's req url (x-forwarded-host) should be  
      changed  to  beta.domain.com.

And i want to redirect all traffic after /app1/ to somedomain.com ..... i don't want to redirect appp1

I'm running out of ideas to fix it...please help me to fix it
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 4

Expert Comment

by:Ryan Weaver
ID: 36522740
I think that in this example, the following:
RewriteRule ^/app1/$       %1      [NC,L,R=301]
RewriteRule ^/app1/$       http://beta.domain.com/%1      [NC,L,R=301]

Open in new window


should be
RewriteRule ^/app1/$       $1      [NC,L,R=301]
RewriteRule ^/app1/$       http://beta.domain.com/$1      [NC,L,R=301]

Open in new window

0
 
LVL 4

Expert Comment

by:Ryan Weaver
ID: 36522753
In the original example I believe:
rewriteRule ^(.*) http://beta.domain.com%{REQUEST_URI}

Open in new window

Should be
RewriteRule ^(.*) http://beta.domain.com$1

Open in new window

0
 

Author Comment

by:rameshsukhi123
ID: 36523495
After doing the above changes , when I'm hitting

http://alpha.domain.com/app1/res/testapp/superbrand/320/banner2.png

it's going to :  http://beta.domain.com/

But it should go to  :  http://beta.domain.com//res/testapp/superbrand/320/banner2.png

In the redirect log i'm getting :



xx.xx.xx.xx - - [12/Sep/2011:15:30:57 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (2) init rewrite engine with requested uri /
xx.xx.xx.xx - - [12/Sep/2011:15:30:57 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (3) applying pattern '^' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:30:57 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:30:57 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (3) applying pattern '^/app1/$' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:30:57 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (1) pass through /
xx.xx.xx.xx - - [12/Sep/2011:15:30:58 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (2) init rewrite engine with requested uri /
xx.xx.xx.xx - - [12/Sep/2011:15:30:58 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (3) applying pattern '^' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:30:58 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:30:58 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (3) applying pattern '^/app1/$' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:30:58 +0000] [beta.domain.com/sid#55555588b478][rid#555555b16730/initial] (1) pass through /
xx.xx.xx.xx - - [12/Sep/2011:15:30:59 +0000] [beta.domain.com/sid#55555588b478][rid#555555b1a750/initial] (2) init rewrite engine with requested uri /
xx.xx.xx.xx - - [12/Sep/2011:15:30:59 +0000] [beta.domain.com/sid#55555588b478][rid#555555b1a750/initial] (3) applying pattern '^' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:30:59 +0000] [beta.domain.com/sid#55555588b478][rid#555555b1a750/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:30:59 +0000] [beta.domain.com/sid#55555588b478][rid#555555b1a750/initial] (3) applying pattern '^/app1/$' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:30:59 +0000] [beta.domain.com/sid#55555588b478][rid#555555b1a750/initial] (1) pass through /
xx.xx.xx.xx - - [12/Sep/2011:15:31:00 +0000] [beta.domain.com/sid#55555588b478][rid#555555b10700/initial] (2) init rewrite engine with requested uri /
xx.xx.xx.xx - - [12/Sep/2011:15:31:00 +0000] [beta.domain.com/sid#55555588b478][rid#555555b10700/initial] (3) applying pattern '^' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:31:00 +0000] [beta.domain.com/sid#55555588b478][rid#555555b10700/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:31:00 +0000] [beta.domain.com/sid#55555588b478][rid#555555b10700/initial] (3) applying pattern '^/app1/$' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:31:00 +0000] [beta.domain.com/sid#55555588b478][rid#555555b10700/initial] (1) pass through /



0
 
LVL 4

Expert Comment

by:Ryan Weaver
ID: 36523587
Try
RewriteRule ^/app1/(.*)$       http://beta.domain.com/$1      [NC,L,R=301]

Open in new window

0
 

Author Comment

by:rameshsukhi123
ID: 36523760
Still getting the same

http://alpha.domain.com/app1/res/testapp/superbrand/320/banner2.png

it's going to :  http://beta.domain.com/

Below are the logs :

xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (2) init rewrite engine with requested uri /
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^/app1/$' to uri '/'
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (1) pass through /
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (2) init rewrite engine with requested uri /res/testapp/superbrand/320/_sp.css
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^' to uri '/res/testapp/superbrand/320/_sp.css'
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^/app1/$' to uri '/res/testapp/superbrand/320/_sp.css'
xx.xx.xx.xx - - [12/Sep/2011:15:56:11 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (1) pass through /res/testapp/superbrand/320/_sp.css
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (2) init rewrite engine with requested uri /res/testapp/superbrand/320/submit.jpg
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^' to uri '/res/testapp/superbrand/320/submit.jpg'
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^/app1/$' to uri '/res/testapp/superbrand/320/submit.jpg'
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (1) pass through /res/testapp/superbrand/320/submit.jpg
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (2) init rewrite engine with requested uri /res/testapp/superbrand/320/banner2.png
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^' to uri '/res/testapp/superbrand/320/banner2.png'
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^/app1/$' to uri '/res/testapp/superbrand/320/banner2.png'
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (1) pass through /res/testapp/superbrand/320/banner2.png
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (2) init rewrite engine with requested uri /res/testapp/superbrand/320/strip.png
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^' to uri '/res/testapp/superbrand/320/strip.png'
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (4) RewriteCond: input='beta.domain.com' pattern='alpha.domain.com' => not-matched
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (3) applying pattern '^/app1/$' to uri '/res/testapp/superbrand/320/strip.png'
xx.xx.xx.xx - - [12/Sep/2011:15:56:12 +0000] [beta.domain.com/sid#55555588b488][rid#555555b10720/initial] (1) pass through /res/testapp/superbrand/320/strip.png
0
 
LVL 4

Expert Comment

by:Ryan Weaver
ID: 36523832
Three things that I can see from the log pasted:

The pattern that is being applied is '^/app1/$' instead of '/app1/(.*)$'.
The URI that it is applying to the pattern starts with '/res/testapp/superbrand' not '/app1'
The log appears to be of beta.domain.com instead of alpha.domain.com.
0
 

Author Comment

by:rameshsukhi123
ID: 36524044
This is my Apache virtual host config file :

<VirtualHost system02:80>
   ServerName alpha.domain.com
   ErrorLog /var/log/httpd/alpha.error.log
   CustomLog /var/log/httpd/alpha.log combined

   SetEnv force-proxy-request-1.0 1
   SetEnv proxy-nokeepalive 1

   ProxyRequests Off
   ProxyPreserveHost On

Options +FollowSymlinks
RewriteEngine On
RewriteLog /var/log/httpd/alphadomain_rewrite.log
RewriteLogLevel 9

RewriteCond %{HTTP_HOST} alpha.domain.com
RewriteRule ^(.*) http://beta.domain.com$1

RewriteRule ^/app1/$       $1      [NC,L,R=301]
RewriteRule ^/app1/(.*)$       http://beta.domain.com/$1      [NC,L,R=301]

</VirtualHost>


One more thing which i want to add here is that

Below is the process flow

alpha.domain.com/app1/path/to/file (User types this in the browser)
                                 |
beta.domain.com/app1/path/to/file (domain is being changed by the mod_rewrite)
                                 |
beta.domain.com/path/to/file (This is been served by tomcat) We are almost reached here
                                 |
alpha.domain.com/app1/path/to/file (User gets the out put in the same domain , this redirection will be done by tomcat) This we have to achieve.

beta.domain.com is in a different virtual host config file and I'm using Apache reverse proxy for that .

Please let me know if you need more info.

0
 
LVL 4

Accepted Solution

by:
Ryan Weaver earned 500 total points
ID: 36524394
So,

The first RewriteRule tells http://alpha.domain.com/anything to go to http://beta.domain.com/anything.
The second tells http://alpha.domain.com/app1 to go to "" (because $1 hasn't been captured with ()), and don't match any other rules (L = last).
The third tells http://alpha.domain.com/app1/anything to go to http://beta.domain.com/anything, and don't match any other rules (L = last).

If the user facing host is alpha.domain.com and the tomcat app is located on beta.domain.com under a different path, perhaps the best course is proxy pas everything from alpha to beta, then user the rewrite engine to rewrite the urls, but tell the engine to pass the rewrites on through instead of redirecting (the PT flag) RewriteRule Flags

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question