Apache URL rewriting in reverse proxy
Solution 1:
This is how I got it to work. As well as the changes as per my comment to my original question, I needed to exclude .js
and .css
from the rule that added a trailing slash.
<VirtualHost *:80>
ServerAdmin admin@localhost
ServerName mydomain.com
ServerAlias www.mydomain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteLog ${APACHE_LOG_DIR}/rewrite.log
RewriteLogLevel 9
Alias /images/ "/var/www/images/"
RewriteEngine On
# rewrite rule to prevent proxy exploit
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_URI} !^/
RewriteRule .* - [R=400,L]
# consolidate non-www requests onto the www subdomain
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule ^(.*) http://www.yourdomain.com/$1 [R=301,L]
# Add a trailing slash to the URL (ignoring images, CSS and JavaScript)
RewriteCond %{REQUEST_URI} !^/(images)(.*)$
RewriteCond %{REQUEST_URI} !^/(.*)(.js|.css)$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
# proxy to the Jellyfish server (ignoring images)
RewriteCond %{REQUEST_URI} !^/(images)(.*)$
RewriteRule ^(/.*)$ http://app-server:8181/jellyfish$1 [P]
ProxyPassReverse / http://app-server:8181/jellyfish/
# suppress mod_security rules that were giving false positives
SecRuleRemoveById 981059 981060
<Directory "/var/www/images">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Solution 2:
Have you tried:
ProxyPassMatch ^/(.*) http://granny-server:8181/$1
ProxyPassReverse / http://granny-server:8181
Or even more simply:
ProxyPass / http://granny-server:8181/
ProxyPassReverse / http://granny-server:8181/
I wrote up how I deal with Apache reverse proxying and Tomcat over here if you want to compare/contrast what you setup with what I use.
You might want to add this to add a trailing slash to the URL:
# Settings for adding a trailing slash to the URL
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]