How to forward requests to another URL? - apache

Currently, I have the following rule in my httpd.conf file to forward all requests from port 80 to port 8080 to be served by GlassFish app server:
<VirtualHost *:80>
ServerAdmin admin#myserver.com
ServerName myserver.com
ProxyPreserveHost On
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
Now, I need to add a rule such that all requests to http://myserver.com/ will be forwarded to http://myserver.com/page/index.html and the URL should still appear to be http://myserver.com/ on the client's browser. I tried to add the following rules inside the above VirtualHost:
RewriteEngine On
RewriteRule http://myserver.com/ http://myserver.com/page/index.html
or
RewriteEngine On
RewriteRule ^/ http://myserver.com/page/index.html
or
RewriteEngine On
RewriteRule ^/index.html http://myserver.com/page/index.html
However, when I go to http://myserver.com/, the browser have this error: This webpage has a redirect loop. The 3rd rule can only work if I go to http://myserver.com/index.html.
I am a total noob at writing rules for Apache. Hence, I'd be very grateful if you could show me what I've done wrong here :).
UPDATE:
The following rule works perfectly:
RewriteEngine On
RewriteRule ^/$ /page/index.html [R]

You need to add a $ indicating the end of the URI:
RewriteEngine On
RewriteRule ^/$ http://myserver.com/page/index.html
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
Without the $, the regex ^/ matches /page/index.html which will cause it to redirect again, and it'll match again, and redirect again, etc.

Related

Apache split wildcard sub-domain for ProxyPass

I have Apache config where I would need to split the http host domain which includes dashed subdomain and build a new path using these 3 of these matching groups in proxy pass or rewrite rule.
Example urls:
kube-test-selfservice.example.com/app/
kube-staging-selfservice.example.com/app2/
Would need to proxied to:
balancer://kubernetes/test/selfservice/app/
balancer://kubernetes/staging/selfservice/app2/
It is important that the test and selfservice in this example are captured as these values change. kube can be hardcoded for distinguishing this host under.
I currently only have basic proxy setup, have tried multiple regex rewrites, but as I am not very familiar with apache, would like some advice on that part.
<VirtualHost *:443>
ServerName example.com
ServerAlias *.example.com
ProxyRequests Off
ProxyPreserveHost On
AddDefaultCharset Off
<Proxy "balancer://kubernetes">
BalancerMember http://192.168.1.244:30001 route=node1 timeout=600
</Proxy>
ProxyPass / "balancer://kubernetes/"
ProxyPassReverse / "balancer://kubernetes/"
</VirtualHost>
Please try this, i try to run below and it worked :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^kube-([a-z0-9_]+.)?-([a-z0-9_]+.)?.example.com [NC]
RewriteRule "^/?(.*)" http://kubernetes/%1/%2%{REQUEST_URI} [R=301,L]
Used URL :
http://kube-test-selfservice.example.com/app/
URL Rewritten to :
http://kubernetes/test/selfservice/app/

Apache - Too many redirects due to a RewriteRule

I'am facing a tomcat with apache as a reverse proxy.
URL: http://zvprod.cbc.local/ Works fine.
you've successfully installed Tomcat. Congratulations!
i just want a rewrite or redirect to /zvhtml
from http://zvprod.cbc.local to http://zvprod.cbc.local/zvhtml
Tried different Rules, there is always a browser error "too many redirects"
<VirtualHost *:80>
ServerName zvprod.cbc.local
#RewriteEngine On
#RewriteRule .* http://zvprod.cbc.local/zvhtml [R=301,L]
<Location />
Require all granted
</Location>
HostnameLookups Off
UseCanonicalName Off
ServerSignature On
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://172.22.16.10:60131/
ProxyPassReverse / http://172.22.16.10:60131/
</VirtualHost>
Thanks in advance
RewriteRule .* http://zvprod.cbc.local/zvhtml [R=301,L]
is wrong because it matches all traffic, and then ends in an infinite loop. Instead, try
RewriteRule ^/$ http://zvprod.cbc.local/zvhtml [R=301,L]

Apache proxy pass all urls and rewrite only one specific url

I have a website running the Ghost Blog engine in the back end. I configured the subdomain blog.domain.com to proxy to ghost engine (localhost:2368) but I need to verify that subdomain in google search console so I need the blog.domain.com/googlefile.html to return a specific string (that same string is available at domain.com/googlefile.html). How do I do that?
My virtual host config:
ServerName blog.example.com
ServerAlias *.blog.example.com
#here is what I tried
#RewriteEngine On
#RewriteCond %{HTTP_HOST} blog\.example\.com
#RewriteRule googlefile.html https://example.com/googlefile.html
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http://127.0.0.1:2368/
Btw. all domains are https.
The solution was to enable SSLProxyEngine so I can proxy https urls and also use mod_rewrite with proxy ignore url
SSLProxyEngine On # enable SSLProxyEngine
ServerName blog.example.com
ServerAlias *.blog.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} blog\.example\.com
RewriteRule googlefile.html https://example.com/googlefile.html [P]
ProxyPreserveHost On
ProxyPass googlefile.html ! # ignore the rewrited url
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http://127.0.0.1:2368/

Why doesn't the URL rewrite doesn't handle the non-WWW to www?

I am using Apache server for URL redirects.
I'm making url redirects in 00_application.conf of Apache at /etc/httpd/conf.d/elasticbeanstalk in AWS
<VirtualHost *:80>
<Proxy *>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/ retry=0
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost on
RewriteEngine On
RewriteCond %{REQUEST_URI} !^\/qqd [NC,OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.example.com/qqd/ [L,R=301]
ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
The second RewriteCond in above code doesn't work.
But first RewriteCond does work.
Following example doesn't work either.
<VirtualHost *:80>
ServerName example.com
Redirect / http://www.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
. . .
. . .
</VirtualHost>
I want non-WWW URLs to be redirected to www.example.com/qqd
Please help.
PS: When I do mywebsite.com without www, this is what I see
https://i.stack.imgur.com/wWIqD.png
I am wondering, when I do mywebsite.com without www, whether that request is hitting my server (or Amazon Route 53 Hosted zone --> web server --> app sever).
I thought this might be an additional info.
Ok, I found the answer.
Yep, My guess was right.
When I do www.example.com it hits my webserver --> URL reroute logic.
But when I do example.com in browser, it doesn't hit my server at all.
I was getting
https://qph.ec.quoracdn.net/main-qimg-99c326b3c6af3d7101645af4a12516b9
We need to have an A record for both
www.example.com as well as
example.com
and make sure both point to web server where you have your URL redirect logic.
Good luck.

apache mutile host reverse proxy rewrite

I would like to have apache acting as reverse proxy and redirect URLs to different Host.
<VirtualHost *:80>
ProxyPreserveHost On
RewriteEngine On
RewriteRule ^/app1/(.*) http://192.168.56.102:10001/$1 [P,L]
ProxyPassReverse /app1/ http://192.168.56.102:10001/
RewriteRule ^/(.*) http://192.168.56.102:82/$1 [P,L]
ProxyPassReverse / http://192.168.56.102:82
ServerName servername.local
</VirtualHost>
The code above work well and redirect my URL :
servername.local to port 82
servername.local/app1/ to the port 10001
I would like to have also the servername.local/app1 to redirect to port 10001 but this doesn't work I have to add the / add the end of the URL I tried to add
ProxyPassReverse /app1 http://192.168.56.102:10001/
But it doesn't work is there a way to achieve that ?
Scrap your rewrites and just use ProxyPass. You're confusing what ProxyPassReverse does with what ProxyPass or RewriteRule with the P flag does.
Also, you're not "redirecting" you're proxying. If you want to proxy /app1 then don't include the trailing slash when you setup your rewriterule or proxypass for it.