Apache proxy pass all urls and rewrite only one specific url - apache

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/

Related

redirect and hide port (folder) in apache virtualhost - xwiki

Env: Ubuntu 18, Apache2, XWiki 12.1 on JETTY 9.4
I have xwiki on https://wiki.company.com:8443/xwiki/ and I would like to hide port 8443 and optionaly /xwiki/.
I have tried with virtualhost:
<VirtualHost *:443>
ProxyPreserveHost On
SSLProxyEngine On
SSLProxyCheckPeerCN Off
SSLCertificateFile /etc/ssl/certs/company.crt
SSLCertificateKeyFile /etc/ssl/private/comapny.key
ProxyPass / https://wiki.company.com:8443/xwiki/
ProxyPassReverse / https://wiki.company.com:8443/xwiki/
</VirtualHost>
This similar solution works for many projects but not for xwiki. The problem is that in this solution xwiki has problem with loading some files because it still try to load files from
https://wiki.company.com/xwiki/webjars/wiki%3Axwiki/drawer/2.4.0/css/drawer.min.css
instead of
https://wiki.hl-display.com/webjars/wiki%3Axwiki/drawer/2.4.0/css/drawer.min.css
So maybe is there any other solution like removing xwiki
ProxyPass / https://wiki.company.com:8443/
ProxyPassReverse / https://wiki.company.com:8443/
and redirect user when enter in browser exact https://wiki.company.com to https://wiki.company.com/xwiki/
Or mod rewrite to replace string
https://wiki.company.com/xwiki/hhh/jjj/aaa
To
https://wiki.company.com/hhh/jjj/aaa
?
I have try a lot of settings but without any success :(
OK, I found solution :) By adding 3 lines in VirtualHost
RewriteCond %{HTTP_HOST} wiki.company.com$ [NC]
RewriteCond %{REQUEST_URI} !^/xwiki/(.*)$
RewriteRule ^(.*)$ /xwiki/ [R=301,L]
So now when user enter exact URL: https://wiki.company.com it will redirect to https://wiki.company.com\xwiki\. So wiki works, port is hidden and nice URL works :)
so complete code is:
<VirtualHost *:443>
ProxyPreserveHost On
SSLProxyEngine On
SSLProxyCheckPeerCN Off
SSLCertificateFile /etc/ssl/certs/company.crt
SSLCertificateKeyFile /etc/ssl/private/company.key
RewriteEngine on
ServerName https://wiki.company.com
RewriteCond %{HTTP_HOST} wiki.company.com$ [NC]
RewriteCond %{REQUEST_URI} !^/xwiki/(.*)$
RewriteRule ^(.*)$ /xwiki/ [R=301,L]
ProxyPass / https://wiki.company.com:8443/
ProxyPassReverse / https://wiki.company.com:8443/
</VirtualHost>

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 ProxyPass only for subdomain and folder

I want to use ProxyPass and only apply for the following subdomain and folder:
de.domain.com/blog/ -> http://blog.olddomain.com/de/
Our new page will have multiple subdomains (each per locale). When i only use this:
ProxyPass /blog/ http://blog.olddomain.com/de/
ProxyPassReverse /blog/ http://blog.olddomain.com/de/
All subdomains are using the rule. So my question is, how can i tell ProxyPass only apply this on de.domain.com/blog/ and not on en.domain.com/blog/ etc.
Thank you!
The first option would be to split the VirtualHost configuration, and first have separate virtual host for de.domain.com which will include the above ProxyPass directives, and then have another virtual host for *.domain.com, without ProxyPass:
<VirtualHost *:443>
ServerName de.example.com
ProxyPass /blog/ http://blog.olddomain.com/de/
ProxyPassReverse /blog/ http://blog.olddomain.com/de/
# Other directives here
</VirtualHost>
<VirtualHost *:443>
ServerName *.example.com
# Other directives here
</VirtualHost>
If you don't want to do that (for example because it might make configuration maintenance more complicated) you can try:
RewriteEngine On
RewriteCond %{HTTP_HOST} =de.example.com [NC]
RewriteRule /blog/(.*) http://blog.olddomain.com/de/$1 [NC,P,L]
ProxyPassReverse /blog/ http://blog.olddomain.com/de/

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.