Apache http server rewrite or redirect - apache

I have a functioning environment with an apache http server (load balancer) with a cluster of tomcat servers at the back end. I am stumped by one problem though, I am not able to redirect or rewrite the request url to what I need. for example,
request url - http://www.yyy.com
i would like to redirect or rewrite it to
http://www.yyy.com/mycontext
I tried redirect and rewrite with no luck.
RewriteRule ^/$ http://www.yyy.com/mycontext/ [L] - Error (can't fine the url)
AND then I tried this,
Redirect / http://www.yyy.com/mycontext/ - this results in the following,
http://www.yyy.com/mycontext/mycontext/mycontext/mycontext/mycontext/........... repeats.
I am not able to figure out the right condition statement. I tried using the server variables to write a rewritecond but I am doing something wrong and it gets ignored.
Any assistance is appreciated. I have not had much experience in this area.

And this ProxyPass / http://www.yyy.com/mycontext/ is in the config for www.yyy.com?
yes in the virtualhost
Not sure how your load balancer could ever possibly work. You are proxying requests back to yourself, and it's going to instantly run out of threads because each proxied request runs on its own thread.
Request for / arrives at your virtual host
ProxyPass reverse proxies it to http://www.yyy.com/mycontext/, which is itself
Request for /mycontext/ arrives at your virtual host
ProxyPass reverse proxies it to http://www.yyy.com/mycontext/mycontext/
Request for /mycontext/mycontest/ arrives at your virtual host
ProxyPass reverse proxies it to http://www.yyy.com/mycontext/mycontext/mycontext/
etc.
I think what you're probably looking for is:
RewriteCond %{REQUEST_URI} !^/mycontext/
RewriteRule ^(.*)$ /mycontext/$1 [L]
But this has nothing to do with your load balancer or your tomcat server.

Related

Proxy https requests and add a query param

I am not very experienced with Apache. What I want to do is to have a proxy server that I can use on a browser or with mobile clients to intercept HTTPS traffic to specific domains, then add a query param to the corresponding request and forward it.
With Apache I got it working using mod_rewrite for HTTP requests like so:
RewriteEngine On
RewriteCond %{QUERY_STRING} !PARAM
RewriteRule ^/(.*)$ http://%{HTTP_HOST}/$1?PARAM=SOME_FIXED_VALUE [P,QSA]
But not for https. I guess its related to the encryption and me changing the request.
I own the SSL certificate - so I could use it to offload the traffic if necessary for the change.
I am somehow stuck on how to do it though and if this is possible. Any help would be appreciated. If its not possible with Apache, I am also open for other possibilities.

Redirection https to https within same Apache

I have a requirement where I have to redirect my hostname to particular application which is again hosted on same Apache. Let's take an example, When I hit on host(https://domain1.example.com), It should internally redirect me to Apache Web Application (https://domain1.example.com/application1) without changing the browser URL.
I am not sure how to achieve SSL to SSL redirection. Thanks in Advance..!!!
This should work. This will redirect all incoming urls that are going to domain1.example.com/ to domain1.example.com/application1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.example.com$
RewriteRule ^$ https://domain1.example.com/application1 [L,R=301]
If without changing browsing URL is your goal then PROXY is your way.
Put following in your apache vhost or global file,
ProxyPass https://domain1.example.com/ https://domain1.example.com/application1
ProxyPassReverse https://domain1.example.com/ https://domain1.example.com/application1
PS shibboleth has nothing to do with this, at least you have not mentioned any case.
EDIT
ProxyPass should come to virtural host not in location
Ideally all the location tag should be out of virtual host

HTTPS redirection on AWS ELB

We have web servers running Apache behind an AWS ELB. I have setup the ELB to accept HTTPS connections, and send the requests over HTTP to the webservers. This works fine.
I have also redirected all the requests to ELB from HTTP to HTTPS using HTTP:X-Forwarded-Proto.
I have added the below virtualhost section to my httpd.conf file and restarted Apache. This setup is redirecting HTTP requests to HTTPS but it is landing on the Apache home page instead of the expected site.
ServerName www.myexample.com
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule !/index.html https://%{SERVER_NAME}/%{REQUEST_URI} [R=301,L]
The configuration seems to be simple and straightforward but not working.
Please let me know what is wrong in the above setup and why is it landing on the Apache home page.
You should escape the . in your rewrite rule. Change your Rewrite to be:
RewriteRule "!/index\.html" https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Also, as in the comment to your OP Remove the slash between %{HTTP_HOST}%{REQUEST_URI}

Apache Reverse Proxy - Stop Trailing Slash Redirection

I'm migrating a site running static HTML on a Sun One (iPlanet) 6.1 to a Drupal site on Apache.
There is a requirement to keep the old server running and serving old content until it can be moved into Drupal. I figured the easiest way is to have some reverse proxy entries in Apache that point to this old content.
Here is what I have in the httpd.conf at the server context level.
RewriteEngine On
RewriteLogLevel 9
RewriteLog "/var/log/httpd/rewrite_log"
RewriteRule offices/ist/?(.*)$ http://oldserver/offices/ist/$1 [P]
<Location /offices/ist/>
ProxyPassReverse /
</Location>
When I make a request like this everything works OK.
http://newserver/offices/ist/somedir/
If I make the same request (without the trailing slash) it 302 redirects using the old server's name.
http://newserver/offices/ist/somedir => Redirect to http://oldserver/offices/ist/somedir/
Shouldn't Apache catch this redirect before it's sent back to the client?
Can I get Apache to rewrite these requests before they're sent to the old server?
I'm not sure, but may be (.*) it too greedy.
I had a look at the Apache mod_rewrite but I did not find an example with ?.
I would suggest to try it without the /? or with something similar to the Apache mod_proxy example.
ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar
The problem is a result of Sun One's redirection headers.
Proxying Apache to another Apache server, the problem goes away. Apache is able to catch the redirection sent by the backend Apache server before it is sent to the client.

Apache mod rewrite to direct from one server to another

I've got to apache web servers running. One on port 80 and another on port 8077.
I'm wanting to try and redirect all the traffic through the port 80 version if possible.
What I'm ideally wanting is to be able to go to http://translate.example.com
and the traffic get directed to http://www.example.com:8077
I've already got a lot of mod_rewrite going on at the main port 80 server, but I'm not sure which of the servers needs configuration or whether both do.
I'm wanting to make sure that translate.example.com/img (or any other subdirectory) actually points to the 8087/images directory.
update
I've now got the following:
RewriteCond %{HTTP_HOST} example[NC]
RewriteCond %{REQUEST_URI} ^/glot$ [NC]
RewriteRule ^(.*)$ http://www.example.com:8077/$1 [P]
ProxyPassReverse / http://www.example.com/
I'm getting to see the other servers new pages, but I'm finding all the resources aren't found like images, css etc
Doing a view source all the resources in the installed product are set with leading slash
For example
/img/glotpress-logo.png
So I'm not sure how to get the resources loaded up.
Note I'm happy enough if the original starting point is www.example.com/glot instead of glot.example.com as in the original question
You can remap your resources to another server but having the other server on non-default port might prevent some of your visitors from viewing them as they might be block (firewalled) from accessing the port. If you're not worry about the port being block you can use mod_rewrite Remapping Resources. method.
RewriteEngine On
RewriteRule ^/images/(.+) http://www.example.com:8077/images/$1 [R,L]
If you want to make sure that everyone is able to view the external resources, you need to use proxy where apache will tunnel the visitor connection to example.com:8077 transparently. You can read more about mod_rewrite for Proxying at apache website.
RewriteEngine on
RewriteBase /images/
RewriteRule ^images/(.*)$ http://example.com:8077/images/$1 [P]
ProxyPassReverse /images/ http://example.com/images/
UPDATE
Have you tried to remove
RewriteCond %{HTTP_HOST} example[NC]
This line basically tells that it will only process if the HTTP_POST is example.com if its coming from www.example.com this rule is not applicable. I'm hoping that "example[NC]" is a typo.
In the end it probably looks like
RewriteRule ^/glot/(.*)$ http://www.example.com:8077/glot/$1 [P]
ProxyPassReverse /glot/ http://www.example.com:8077/glot/
You need to do the configuration at the port 80 server to make it act as a proxy for the 8077 server.
The Apache document is here: http://httpd.apache.org/docs/trunk/rewrite/proxy.html