Rewrite rule to hide port from URL of Rails server? - apache

I have a rails server running on URL "http://example.com:1234"
I want to provide the URL of this application to an user as "http://example.com/myapp" so that Apache (or Rack or whatever you suggest that works) can redirect request for "/myapp" to port 3333 of that domain.

Take a look at setting up a Reverse Proxy under apache.
Apache, listening to port 80 on example.com, would reverse proxy to port 1234. Then requests for http://example.com/myapp would be internally proxied to http://example.com:1234/myapp (or however you setup your ProxyPass target).
If you don't have access to server config, you can use mod_rewrite's Proxy flag and setup some rules inside an .htaccess file. Something along the lines of:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^myapp(.*) http://example.com:1234/$1 [P,L]

Somethimes it loose it css style. You can use only:
RewriteRule ^myapp(.*) http://example.com:1234 [L,R]

Related

apache rewrite rule for http_host containing port

In the local network, I have synology server with a number of services running in docker on different ports and accessible in-browser like that
192.168.1.2:8989 or server.spb.lan:8989
How to make a rewrite rule to convert them like that 192.168.1.2/servicename or server.spb.lan/servicename?
Like that
192.168.1.2:8989 -> 192.168.1.2/servicename
server.spb.lan:8989 -> server.spb.lan/servicename
Based on your shown samples, could you please try following. Please make sure you clear your browser cache after placing these rules in your htaccess file.
RewriteEngine ON
RewriteCond %{HTTP_HOST} 192\.168\.1\.2
RewriteCond %{REQUEST_URI} ^/servicename [NC]
RewriteRule ^ http://%{HTTP_HOST}:8989%{REQUEST_URI} [NE,L]
RewriteCond %{HTTP_HOST} server\.spb\.lan
RewriteCond %{REQUEST_URI} ^/servicename [NC]
RewriteRule ^ http://%{HTTP_HOST}:8989%{REQUEST_URI} [NE,L]
I assume, that those backend services operate based on the http protocol, since you did not specify anything else...
Easiest probably is to use the apache proxy module to expose those backend services. You can use it within the rewriting module which makes the approach pretty convenient:
RewriteEngine on
RewriteRule ^/?servicename(/.*)$ https://server.spb.lan:8989$1 [P]
An alternative to the rewriting module would be to implement a reverse proxy:
ProxyRequests off
ProxyPass /servicename https://server.spb.lan:8989
ProxyPassReverse /servicename https://server.spb.lan:8989
I suggest you read about the details in the apache documentation. As typical for OpenSource it is of excellent quality and comes with great examples.
I couldn't solve the issue with apache installed directly on Synology. So I deleted it, deleted web station. Then I install nginx in docker. Synology has integrated nginx running on port 80, which cannot be deleted and there is no access to its settings.
So I just mapped internal port 80 of nginx container to port 82 on Synology, make forward port 80 from router to port 82 on Synology.
And then in config of nginx container I did simply that for each of my app running in docker
server {
server_name sonarr.lan;
location / {
proxy_pass http://192.168.1.2:8989;
}
}

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 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

Apache mod_rewrite RewriteRule incorrectly adding in port AWS

I have an issue where we are running two systems on the same EC2 instances and using AWS Elastic Load Balancer to send request for one systems to port 81.
So for example we have www.example.com and bookings.example.com where the AWS Elastic Load Balancer sends requests for bookings. though to our EC2 boxes on port 80 and request for www. get sent though on port 81.
The customer connects to www.example.com on port 80 but then gets from the load balancer to the server on port 81.
When we add this rule for example to the .htaccess for the www site we have an issues with port 81 appearing.
RewriteRule ^index.html / [R=301,L,QSA] !-s
#Results in customer being sent to http://www.example.com:81/
How can I make sure the port 81 is not pushed back to the customer?
I have come up with this alternative:
RewriteRule ^index.html http://%{HTTP_HOST}/ [R=301,L,QSA] !-s
But in this example http is hard coded and I would need to make that variable so it can be https when needed. I also have more then just this index.html rule that redirect back to / I have about 30 rules and feel there should be a one liner to fix this passing port 81 back to the customer.
Your alternative will work and could be extended for ssl support, but it's probably not necessary. Apache follows a specific order when creating self referencing URL's. UseCanonicalName, and UseCanonicalPhysicalPort control how they are created regardless of the module (mod_rewrite, mod_alias, etc).
Without knowing more of your configuration, I would suggest starting with these directives in the appropriate VirtualHost.
UseCanonicalName On
ServerName www.example.com:80
EDIT: If you don't want to do this for whatever reason, here's how you fix your rewrite to support ssl.
RewriteCond %{HTTPS} =on
RewriteRule - - [env=req_scheme=http,S=1]
RewriteRule - - [env=req_scheme=https]
RewriteRule ^index.html %{ENV:req_scheme}://%{HTTP_HOST}/ [R=301,L,QSA]