Mod_proxy rewrite rule help for /foo/ to /foo/index.html - apache

With mod_proxy I need to redirect /foo/ requests to /foo/index.html. So if a user types in : https://example.com/foo/ they should be redirected to https://example.com/foo/index.html
I tried the following rule but it does not work:
<Location /foo/>
RequestHeader set Host example.com
</Location>
ProxyPass /foo/ https://example.com/foo/index.html
ProxyPassReverse /foo/ https://example.com/foo/index.html
While this works for any requests to https://example.com/foo/*, it does not work for https://example.com/foo/. Any help on what I can use for the redirect to work correctly?

You need to add a normal redirect to index.html before the request hits the proxy.
<Location /foo/>
RequestHeader set Host example.com
</Location>
RewriteRule ^/foo(/|)$ /foo/index.html [NC]
ProxyPass /foo/ https://example.com/foo/
ProxyPassReverse /foo/ https://example.com/foo/

Related

Setting up mod_jk redirect in Hybris

I have installed apache httpd 2.2.15 in my app server. I need to get the login page(https://ip_address:9002/xxstorefront/xx/en/USD/login) when I hit on https://dev.xxyy.com/login. I have installed SSL certificate for my domain and set below redirect rules.
ProxyPass /login http://localhost:9001/xxstorefront/xx/en/USD/login
ProxyPassReverse /login http://localhost:9001/xxstorefront/xx/en/USD/login
ProxyPass /login https://localhost:9002/xxstorefront/xx/en/USD/login
ProxyPassReverse /login https://localhost:9002/xxstorefront/xx/en/USD/login
RewriteEngine On
RewriteRule ^(.*)/login http://%{ip_address:9001}$1/{xxstorefront/xx/en/USD/login}$2 [L,R]
When I hit on https://dev.xxyy.com/login, I get below error,
Not Found 
The requested URL /login was not found on this server.
Apache/2.2.15 (CentOS) Server at dev.xxyy.com Port 443
When I hit on https://dev.xxyy.com, I get the apache default homepage.
Pls guide me how should I set the redirect rules.
Your configuration is invalid. Those two lines:
ProxyPass /login https://localhost:9002/xxstorefront/xx/en/USD/login
ProxyPassReverse /login https://localhost:9002/xxstorefront/xx/en/USD/login
overwrite those two:
ProxyPass /login http://localhost:9001/xxstorefront/xx/en/USD/login
ProxyPassReverse /login http://localhost:9001/xxstorefront/xx/en/USD/login
Rewite mechanism probably does not work at all:
RewriteEngine On
RewriteRule ^(.*)/login http://%{ip_address:9001}$1/{xxstorefront/xx/en/USD/login}$2 [L,R]
I think this configuration should solve your problem:
<VirtualHost *:80>
ServerName dev.xxyy.com
ProxyPreserveHost On
ProxyPass / http://localhost:9001/xxstorefront/xx/en/USD/
ProxyPassReverse / http://localhost:9001/xxstorefront/xx/en/USD/
</VirtualHost>
<VirtualHost *:443>
ServerName dev.xxyy.com
SSLEngine on
// other SSL directives
ProxyPreserveHost On
ProxyPass / https://localhost:9002/xxstorefront/xx/en/USD/
ProxyPassReverse / https://localhost:9002/xxstorefront/xx/en/USD/
</VirtualHost>
It defines two virtual hosts which work as proxies and map all requests to xxstorefront/xx/en/USD/...:
http://dev.xxyy.com/(.*) → http://localhost:9001/xxstorefront/xx/en/USD/(.*)
https://dev.xxyy.com/(.*) → https://localhost:9002/xxstorefront/xx/en/USD/(.*)

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/

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.

Apache Rewrite to a Location

We have a single web application that handles different URL formats and returns the same content in a different formats.
/dosomething
/foo/dosomething (returns mobile content)
/bar/dosomething (returns html for a browser)
See the sample Virtual Host below.
For Apache 2.2, I used Rewrite Rules to redirect http://www.foobarbaz.net/dosomething to the Location "/baz/" which functioned as I expected.
The "PT" flag on the RewriteRule passed the URI back to Apache and allowed us to serve content from the "baz" location.
<VirtualHost *:80>
ServerName www.foobarbaz.net
RewriteEngine On
#Block requests for favicon
RewriteRule ^/favicon.ico$ - [F]
# If the requested URI is NOT located in bar or foo
# Prepend an /baz to everything that does not already starts with it
# and force the result to be handled by the next URI-handler ([PT])
RewriteCond %{REQUEST_URI} !^/bar/.*$
RewriteCond %{REQUEST_URI} !^/foo/.*$
RewriteRule ^/(.*) /baz/$1 [PT,QSA]
<Location "/baz/">
RequestHeader append ABC-Request-Origin "/baz"
ProxyPass ajp://localhost:8009/webapp/
ProxyPassReverse /
ProxyPassReverseCookiePath /webapp /
</Location>
<Location "/bar/">
RequestHeader append ABC-Request-Origin "/bar"
ProxyPass ajp://localhost:8009/webapp/
ProxyPassReverse /
ProxyPassReverseCookiePath /webapp /bar/
</Location>
<Location "/foo/">
RequestHeader append ABC-Request-Origin "/foo"
ProxyPass ajp://localhost:8009/webapp/
ProxyPassReverse /
ProxyPassReverseCookiePath /webapp /foo/
</Location>
</VirtualHost>
For Apache 2.4.12, the Rewrite Rules do not function in the same manner and Apache tries to find the content on the file system. Apache looks for a "baz" directory which does not exist.
I tried adding another location "/" and removing the rewrite rules (see below).
The docs indicate that for overlapping Locations, the least specific should go first.
http://httpd.apache.org/docs/current/sections.html
This works for "/", but not for "/foo/" or "/bar". It seems that "/" is always used and "foo" and "bar" are always included in the request to the p.roxy
<VirtualHost *:80>
ServerName www.foobarbaz.net
<Location "/">
RequestHeader append ABC-Request-Origin "/baz"
ProxyPass ajp://localhost:8009/webapp/
ProxyPassReverse /
ProxyPassReverseCookiePath /webapp /
</Location>
<Location "/bar/">
RequestHeader append ABC-Request-Origin "/bar"
ProxyPass ajp://localhost:8009/webapp/
ProxyPassReverse /
ProxyPassReverseCookiePath /webapp /bar/
</Location>
<Location "/foo/">
RequestHeader append ABC-Request-Origin "/foo"
ProxyPass ajp://localhost:8009/webapp/
ProxyPassReverse /
ProxyPassReverseCookiePath /webapp /foo/
</Location>
</VirtualHost>
Does anyone have suggestions on how to improve this setup? Or suggestions on handling requests to "/" differently that "/foo" or "/bar"
Thanks for any help.
Jim
It appears that using a Location "/" first does properly use the Locations for "/", "/foo" and "/bar" I do have other issues related to building links in the application, but I do not believe it is an Apache issue.

How to forward requests to another URL?

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.