How to setup proxy in Apache for REST - ajax

I'm trying to setup a simple proxy in Apache for RESTful services that I have on a different server. For example, if I go to https://myclient.com/services/hello it will show me the JSON for the URL https://myserver.com/services/hello.
I'm doing this to get cross-domain ajax working. I've found a lot of information about how to set this up, but none of the suggestions I have found work. I thought this should be fairly straightforward, so it's probably an easy answer for someone.
My current settings are forwarding me to the RESTful URL instead of just showing the content. I'm using a basic Apache setup without virtual hosts and configuring everything in my httpd.conf. I am using SSL and also am using a proxy to Tomcat on the same Apache server:
SSLProxyEngine On
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]
ProxyPass /services/ https://myserver.com/services/
ProxyPassReverse /services/ https://myserver.com/services/
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
I've tried a combination of a lot of other settings as well, including an addition RewriteRule, but this simple configuration is the closest I've gotten.

The problems were due to a SSO server that I had setup - requests were being redirected through this server, and this caused a redirect from the SSO server to the API server. The apache settings were correct.

Related

Why is rewriteRule in httpd.conf not working on this Apache proxy server?

I recognize that this question is similar to others on this site. I have spent days reading these posts and trying different approaches. None has worked.
I am trying to use an Apache proxy server to map a particular URL to a new URL. I have a dedicated device (Black Box) on a local network which makes calls to original.remoteHost1.com. I wish to use the Apache proxy server on this local network to substitute new.remotehost2.com for original.remoteHost1.com when Black Box requests service. I am able to configure Black Box to point to the proxy server but Black Box does not expose an interface to change its target host for the HTTP service requests.
Black Box makes only two very specific requests:
http://original.remoteHost1.com/rectrack/file_name.php?data_1=text_string_1
http://original.remoteHost1.com/rectrack/file_name.php?data_1=text_string_1&data_2=text_string_2
which I wish to change to:
http://new.remotehost2.com/rectrack/file_name.php?data_1=text_string_1
http://new.remotehost2.com/rectrack/file_name.php?data_1=text_string_1&data_2=text_string_2
Acting as a proxy, the Apache server accepts the Black Box requests and forwards them to original.remoteHost1.com. I expected that I would be able to use rewriteRule in httpd.conf to change original.remoteHost1.com to new.remotehost.com, but I cannot get this seeming simple function to work.
I have tried using the following rewriteRule (as well as many variations) to substitute new.remotehost.com for original.host.com, but I can't seem to get Apache to actually make the substitution:
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
RewriteCond %{HTTP_HOST} original.remoteHost1.com
RewriteRule ^(.*) http://new.remotehost2.com/$1 [P]
Does anyone have any idea why the new URL is not substituted for the original?
I was able to solve this problem. I don't recall why I tried this and I certainly don't know why it works. I'm running Windows 7. In the folder C:\Windows\System32\Drivers\etc\ is a file named "hosts". In that file, in a section related to localhost name resolution, I added the line:
127.0.0.1 original.remoteHost1.com
After doing this, the rewriteCond and rewriteRule acted as I had expected them to.

How to redirect example.com and example.com/anything to example.com/blog

I want to redirect example.com and example.com/anything to example.com/blog. Please note few things.
I refer example.com for a 1 domain.
I use apache as web server.
My document root is set to /var/www/html/public within apache vhost conf file (For a laravel APP).
I tried setting redirects in .htaccess and using apache vhost conf file and I get redirect too many times error.
Can someone help me to accomplish this please?
This probably is what you are looking for: rewriting on the level of the http server:
RewriteEngine on
RewriteRule ^/?$ /blog [R=301]
RewriteRule ^/?anything/?$ /blog [R=301]
If by "anything" you actually mean anything so that a redirection should get applied regardless of the requested path, then this should do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^ /blog [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
You can do that in your routes
// web.php
Route::redirect('/', '/blog');
Route::redirect('/anything', '/blog');

Heroku (PlayFramework/Scala) app automatically redirect to https

I have an PlayFramework App developed using Scala running on Heroku; I only mention the development language and framework because any posts I've found regarding this issue relate to PHP! I have http and https running on a custom domain but I would like to force http requests to be redirect to https.
I've found that I need to update the .htaccess file with the following:
##Force SSL
#Normal way (in case you need to deploy to NON-heroku)
RewriteCond %{HTTPS} !=on
#Heroku way
RewriteCond %{HTTP:X-Forwarded-Proto} !https
#If neither above conditions are met, redirect to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But I am not sure if it is possible or how to set up the .htaccess file using Play and Scala.
Please can someone advise? Thanks.
All you need to do is to add
play.filters.enabled += play.filters.https.RedirectHttpsFilter
In your .conf file.
It will redirect all HTTP requests to HTTPS automatically.
It only works in production mode by default. To change that, add :
play.filters.https.redirectEnabled = true
See the RedirectHttpsFilter
documentation for more.

Remove context name from URL - mod_proxy_ajp with Spring Security

I have a app named "FitnessTracker" running on tomcat, post 8080. I am using Spring Security, and everything works fine when I test it directly on tomcat using http://localhost:8080/FitnessTracker
I then configured mod_proxy_ajp on Apache http 2.2, to access using a domain name, below is my VirtualHost configuration:
<VirtualHost *:80>
ServerName www.testing.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
RewriteEngine on
RewriteRule ^/FitnessTracker/(.*)$ /$1 [P]
ProxyPass / ajp://localhost:8009/FitnessTracker/
ProxyPassReverse / ajp://localhost:8009/FitnessTracker/
</VirtualHost>
Now, when I access www.testing.com/ - it gets redirected to www.testing.com/FitnessTracker/login.html
Is there a way to not have /FitnessTracker/ in the URL? I would like to hide the tomcat Context name from the URL.
Ideally, I want the URL to look like www.testing.com/login.html
I know that this is due to the RewriteRule, but without the rewrite rule spring security with mod_proxy doesn't work - since Spring security redirects to login page along with context name.
Please let me know if you have suggestions.
I think you forgot to rewrite the cookie domain and cookie path. Check ProxyPassReverseCookieDomain directive. From the backend perspective the request path still has to look like /FitnessTracker/

mod_rewrite with AJAX applications: possible?

I am trying to run Shell In a Box (link) through another server (the computer running shellinabox is not accessible from the internet) . Ideally I could use ProxyPass in the Apache config to have a reverse proxy. Problem is I can't access the conf file. So I tried using .htaccess and I discover that I cannot use ProxyPass in there. So I tried and used mod_rewrite to do the job. Currently I have the following on the .htaccess file
RewriteEngine On
RewriteRule ^$ http://10.1.13.236:4200/ [P]
However while it displays the title correctly and if I open up the source code I can see there is something in the page, nothing is diplayed on the screen (it remains blank). My suspicion is that there are problems with AJAX and this kind of proxy.
What I am trying to accomplish with the mod_rewrite as close as possible behaviour to ProxyPass (Mirorr a website in a subdirectory).
Is this possible? Is there some other solution (I tried phproxy and khproxy but neither of them is able to display anything)?

Resources