Doing an Apache redirect with unchanged URL - apache

I would like to make a redirect with unchanged URL. I have tried the configuration
<VirtualHost *:80>
ServerName test.localhost
ProxyPreserveHost On
ProxyPass / http://www.example.com/
ProxyPassReverse / http://www.example.com/
</VirtualHost>
but it doesn't work as I get a message in the browser saying "The page isn't redirecting properly". Any clues?

Do a rewrite with the proxy flag P:
<VirtualHost *:80>
ServerName test.localhost
RewriteEngine On
RewriteRule /(.*)$ http://www.example.com/$1 [P]
</VirtualHost>

Related

Proxy not redirecting

I have the following Apache config file. When someone types in http://mywebsite.com it is not redirecting them to https. Why?
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass "/" "http://10.0.1.123/"
ProxyPassReverse "/" "http://10.0.1.123/"
ServerName www.mywebsite.com
ServerAlias mywebsite.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =mywebsite.com
RewriteCond %{SERVER_NAME} =www.mywebsite.com
RewriteRule ^ https://mywebsite.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLProxyEngine on
ProxyPreserveHost On
ProxyPass "/" "http://10.0.1.123:80/"
ProxyPassReverse "/" "http://10.0.1.123:80/"
ServerName www.mywebsite.com
ServerAlias mywebsite.com
ServerAdmin admin_ws1#mywebsite.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.mywebsite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.mywebsite.com/privkey.pem
</VirtualHost>
<VirtualHost *:80>
...
ProxyPass "/" "http://10.0.1.123/"
...
RewriteRule ^ https://mywebsite.com%{REQUEST_URI} [END,NE,R=permanent]
The ProxyPass is telling Apache to work as reverse proxy and forward the request to the real server. The RewriteRule instead is telling Apache to answer the request itself with a redirect to the HTTPS version of the site. Obviously it cannot do both at the same time, so there is a conflict. Please remove Proxy* rules and keep only the Rewrite* rules on port 80.

Configure Apache with multiple ProxyPass and different ports

so i have a config file that states this
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.aaaaaaaa.yyyyyyyyy.co.uk
ServerAlias aaaaaaa.yyyyyyy.co.uk
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.aaaaaaa.yyyyyyyyy.co.uk [OR]
RewriteCond %{SERVER_NAME} =aaaaaa.yyyyyyyyy.co.uk
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.xxxxxx.yyyyyyyy.co.uk
ServerAlias xxxxxxx.yyyyyyyyy.co.uk
ProxyPass / http://localhost:8989/
ProxyPassReverse / http://localhost:8989/
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.xxxxxxxxxxx.yyyyyyyy.co.uk [OR]
RewriteCond %{SERVER_NAME} =xxxxxx.yyyyyyyyyy.co.uk
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
when visiting aaaaaaa.yyyyyyyyy.co.uk it redirects to https which is the desiered effect and launches the correct app.
however when i visit xxxxx.yyyyyyy.co.uk it shows the correct app but does not redirect to https, when i physically type https://xxxx.yyyyyy.co.uk it redirects me to the same app as aaaaaaa.yyyyyyyyyy.co.uk.
how can i make this work!
The main issue here is certbot dosn't play nice with multiple hosts in 1 file.
The steps to correct the problem where;
1 remove all certificates
2 recreate all virtual hosts in there own file within sites-availible
(minus the rewrite portion)
example content of one conf file
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.aaaaaaaa.yyyyyyyyy.co.uk
ServerAlias aaaaaaa.yyyyyyy.co.uk
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
</VirtualHost>
3 after doing this for all virtual hosts (sites) run the command
certbot --apache
and create new certificates for all making sure to select the redirect function at the end.
worked great!

How to configure apache2 subdomains to route to different servers

I am running apache 2.4.7 and presently all my traffic is going to a backend server on 8080 on the same server/instance.
I want my www traffic to go to a new server/instance and all my other subdomains (app, api, etc.) to go to the existing 8080.
Can somebody provide some direction?
Help appreciated.
<VirtualHost *:80>
ProxyPreserveHost On
ServerAdmin webmaster#example.com
ServerName example.com
Redirect "/" "https://www.example.com/"
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html/
Redirect "/ft/" "/"
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
If you want to do an external redirect to the new server for the www subdomain, you'll need to add the following to your configuration, under the other rewrite rules:
RewriteCond ${HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://www.example.com/${REQUEST_URI} [L,R=301]
And you would need to remove the ServerAlias directive. This is also assuming you've already pointed DNS to the new host.
If the new box serving the www content is to be another backed server, you would need another virtual host. Add the following at the top instead:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://other-box/
ProxyPassReverse / http://other-box/
</VirtualHost>
And you still would need to remove the ServerAlias directive.

[URL rewriting Apache]port redirect and keep URL

I need to do a port redirection on my server while retaining the original visible URL.
For example http://localhost/Jupiter/ redirect to http://localhost:8080/ while retaining http://localhost/Jupiter/
Now i have this configuration but url retain don't work :
<VirtualHost *:80>
RewriteEngine On
RewriteRule ^(.*)/Jupiter/ http://%{SERVER_NAME}:8080/ [P]
</VirtualHost>
I tried with flag [R=301,L] but it don't work too
On first configuration I haved :
<VirtualHost *:80>
ProxyRequests On
ProxyPass /Jupiter/ http://%{SERVER_NAME}:8080/
ProxyPassReverse /Jupiter/ http://%{SERVER_NAME}:8080/
</VirtualHost>
But this make a 404 for pictures and css
I'm interested of all solutions.

Apache redirect to a port and mask the URL

I have one web server running two sites on different ports.
IE: server:8081 and server:8083
I setup two DNS records and pointed it to “my server”
Dev.server.com and Pre.server.com
I would like Dev.server.com to redirect to server:8083 but mask the URL to always stay Dev.server.com and Pre.server.com to redirect to server:8081 but mask the URL to always stay pre.server.com
If I set them up like this
<VirtualHost *:80>
ServerName http:// Dev.server.com
ProxyRequests off
ProxyPass / http://server:8083
ProxyPassReverse / http://server:8083
</VirtualHost>
<VirtualHost *:80>
ServerName http:// Pre.server.com
ProxyRequests off
ProxyPass / http://server:8081
ProxyPassReverse / http://server:8081
</VirtualHost>
Everything routes to the Dev instance and nothing makes it to the Pre instance
I have it set like this;
<VirtualHost *:80>
ServerName http:// Dev.server.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^dev\.server\.com$ [NC]
RewriteRule ^(.*)$ http:// Dev.server.com:8083$1 [R]
RewriteCond %{HTTP_HOST} ^pre\.server\.com$ [NC]
RewriteRule ^(.*)$ http://pre. server.com:8081$1 [R]
</VirtualHost>
Listen 0.0.0.0:8083
Listen 0.0.0.0:8081
<VirtualHost *:8083>
ServerName dev. server.com
ProxyRequests off
ProxyPass / http:// server.com:8083/jde/owhtml/
ProxyPassReverse / http:// server.com:8083/jde/owhtml/
Oc4jMount /jde HTML_DV_8083
Oc4jMount /jde/* HTML_DV_8083
</VirtualHost>
<VirtualHost *:8081>
ServerName pre.server.com
ProxyRequests off
ProxyPass / http:// server.com:8081/jde/owhtml/
ProxyPassReverse / http:// server.com:8081/jde/owhtml/
Oc4jMount /jde HTML_PY_8081
Oc4jMount /jde/* HTML_PY_8081
</VirtualHost>
This works perfectly for the routing but does not mask the URL. It adds the port to the URL witch we do not want to happen.
Anyone have any ideas as to what I am doing wrong?
You want your reverse proxy to happen in your port 80 vhost. Because you're using mod_rewrite to redirect the browser to URLs like http://Dev.server.com:8083/, that's what the browser will see. You just need 2 vhosts on port 80:
<VirtualHost *:80>
ServerName dev.server.com
ProxyRequests off
ProxyPass / http://server.com:8083/jde/owhtml/
ProxyPassReverse / http://server.com:8083/jde/owhtml/
Oc4jMount /jde HTML_DV_8083
Oc4jMount /jde/* HTML_DV_8083
</VirtualHost>
<VirtualHost *:80>
ServerName pre.server.com
ProxyRequests off
ProxyPass / http://server.com:8081/jde/owhtml/
ProxyPassReverse / http://server.com:8081/jde/owhtml/
Oc4jMount /jde HTML_PY_8081
Oc4jMount /jde/* HTML_PY_8081
</VirtualHost>
Note that the "ServerName" is dev.server.com and pre.server.com, and not http:// Dev.server.com with a space following the scheme and ://. Because http:// Dev.server.com isn't going to be the hostname you're going to visit, apache defaults everything to the first vhost. This is probably why your second attempt works, because both dev and pre default to the first vhost since nothing matches on port 80.