I'm looking for an equivalent of Redirect requests only if the file is not found? where instead of redirecting, I want it to do a ProxyPass to another server if a file is missing.
This didn't work too well since it will not handle requests that do not end with "/" e.g. https://site.trajano.net/trajano which goes to the proxy and redrects instead.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
RewriteRule (.*) https://trajano.github.io/%{REQUEST_URI} [P]
I tried the following from a different answer as well which works slightly better but what happens is it redirects to github rather than proxy.
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
#
# Requested resource does not exist, do rewrite if it exists in /archive
RewriteCond %{DOCUMENT_ROOT}/archive/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/archive/$1 -d
RewriteRule (.*) /archive/$1 [L]
#
# Else
RewriteRule (.*) https://trajano.github.io/%{REQUEST_URI} [P]
The "ProxyPass" and "ProxyPassReverse" parameters are used to tell Apache how to proxy requests. They require the "mod_proxy.so" and "mod_proxy_http.so" Apache modules, which are loaded by default in RHEL5 and RHEL6, but check the following lines are uncommented in the "/etc/httpd/conf/httpd. conf" file to make sure.
Apache Default timeout is set to 300 seconds. This issue comes only when accessing website through apache reverse proxy. It works well by using internal IP.
The ProxyPreserveHost directive is used to instruct Apache mod_proxy, when acting as a reverse proxy, to preserve and retain the original Host: header from the client browser when constructing the proxied request to send to the target server.
When you send a web request, your request goes to the proxy server first. The proxy server then makes your web request on your behalf, collects the response from the web server, and forwards you the web page data so you can see the page in your browser.
Both mod_rewrite
and mod_proxy
need to be enabled. Place the following in a VirtualHost
block (not inside a Directory
directive or anything other than VirtualHost
):
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [L]
#
# Else proxy
RewriteRule ^/(.*)$ https://trajano.github.io/$1 [P,QSA]
ProxyPassReverse / https://trajano.github.io/
The first Rewrite block handles files and directories that exist in which case it returns the original file block.
The next rewrite rule will handle the proxies. The ProxyPassReverse
ensures that the Location
being returned is rewritten back to the local server rather than the proxied server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With