using regex for proxypass - regex

I'm trying to set up a proxy to my corporate environment's ActiveMQ admin pages on our standard http proxy server. I have a rule in place that allows me access to the admin landing page:
ProxyPass /foobar hostname:8161/admin
ProxyPassReverse /foobar hostname:8161/admin
However, going to the "queues" page bring me to a different page appended with a unique session ID (admin/queues.jsp;jsessionid=oq37zgvxz4zkwliwdwddyon3), and I would like for this page to be accessible through the same URL as well.
Attempting to add a wildcard (*) at the end of these proxypass rules breaks the redirect. Is there a redirect rule that I need to use in conjunction with this, and how do I get any string to this host to pass through this proxy rule?

There is ProxyPassMatch that is regex based, but ProxyPass does prefix matching (i.e. a ProxyPass for /foo should catch /foo/bar too) so your current rule should work anyway.
It may be that the redirection URL doesn't match the reverse rule for some reason. I have to admit I've never managed to get a ProxyPass to Tomcat to work correctly when the front and back end context paths differ, so my advice would be to deploy the back end app at hostname:8161/foobar instead of /admin if that's an option.

Related

Reverse Proxying Google Cloud Storage public URL fails / 404s

I'm trying to load some assets from Google Cloud Storage (GCS) onto a website, but I want to use a proxy so if I visit mysite.com/page/asset.html I want it to load the content from http://storage.googleapis.com/my-bucket/directory/asset.html without redirecting.
In my apache configs, I am utilizing mod_proxy
ProxyPass "/page/asset.html" "http://storage.googleapis.com/my-bucket/directory/asset.html"
ProxyPassReverse "/page/asset.html" "http://storage.googleapis.com/my-bucket/directory/asset.html"
If I visit /page/asset.html I get a 404 NoSuchBucketThe specified bucket does not exist.
I'm using HTTP as its just the development environment - I tested this by replacing the GCS URL with a URL from another development environment and that worked. I am thinking that there is some config that I've missed out for proxying GCS.
Note that the GCS URLs are public and can be visited without authentication.
For anyone else that comes across this issue
The problem is that I had ProxyPreserveHost On as it was serving some of my other proxies, but to access GCS we want to switch that off.
Now I can access the assets fine.
So my code looks like:
ProxyRequests Off
ProxyPreserveHost On
ProxyPass /first/proxy https://external.com/fproxy/
ProxyPassReverse /first/proxy https://external.com/fproxy/
ProxyPreserveHost Off
ProxyPass "/page/asset.html" "http://storage.googleapis.com/my-bucket/directory/asset.html"
ProxyPassReverse "/page/asset.html" "http://storage.googleapis.com/my-bucket/directory/asset.html"

Apache mod_alias RedirectMatch everything except specific pattern

Good old regular expressions are driving me nuts.
I need to redirect all traffic in Apache 2.4 from HTTP to HTTPS, except for "/bt/sub/[a_few_endings]", using Redirect from mod_alias (can't use mod_rewrite).
I tested the following regular expression in all online testers I know (e.g. http://regex101.com/) and all confirm that the regex should indeed match everything except the URLs I don't want it to match:
^/(?!bt/sub/(went_active|success|cancel|expired)).*$
As far as I can tell, this should match everything in http://local.mysite.com and redirect it to https://local.mysite.com, except for the following four:
http://local.mysite.com/bt/sub/went_active
http://local.mysite.com/bt/sub/success
http://local.mysite.com/bt/sub/cancel
http://local.mysite.com/bt/sub/expired
Still, Apache redirects everything, including the above URLs I don't want redirected.
I found several similar questions in SO but most of them are answered in the light of mod_rewrite, which is not what I want/need, and the ones that people say have worked have not worked for me.
Here's my virtual host configuration as it currently stands:
<VirtualHost *:80>
ServerName local.mysite.com
RedirectMatch 302 ^/(?!bt/sub/(went_active|success|cancel|expired)).*$ https://local.mysite.com
DocumentRoot /home/borfast/projects/www/mysite/public
#Header set Access-Control-Allow-Origin *
SetEnv LARAVEL_ENV localdev
<Directory /home/borfast/projects/www/mysite/public/>
Options All
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Please help and prevent me from going crazy :)
UPDATE:
There's something weird going on: apparently when the requested URL/path can be found, Apache ignores the expression in RedirectMatch and redirects the client, even though the RedirectMatch tells it not to.
To test this I created a new virtualhost from scratch inside a separate VM freshly installed with Ubuntu Trussty 64, loaded with Apache 2.4. This new virtual host contained just the ServerName, RedirectMatch and DocumentRoot directives, like this:
<VirtualHost *:80>
ServerName testing.com
RedirectMatch 302 ^/(?!bt/sub/(went_active|success)$).*$ https://othersite.com/
DocumentRoot /home/vagrant/www
</VirtualHost>
I created the directory /home/vagrant/www/bt/sub/went_active to make sure Apache could get to at least one of the two possible URLs. When trying to access http://testing.com:8080, I get redirected, as expected.
Then the weirdness comes: when accessing http://testing.com:8080/bt/sub/went_active, the URL that matches the directory I created, I am still redirected, even though I shouldn't be, but when accessing http://testing.com:8080/bt/sub/success, I don't get redirected and instead get a 403 Forbidden.
I may be losing my sanity over this but it seems that when Apache sees that it could serve the request and it matches the regular expression in RedirectMatch that should prevent the redirect, it decides to ignore the regular expression and do the redirect anyway. Three letters for this: WTF?!?!?!
As it was said in comments - it is easier to do with mod_rewrite. Possible solutions
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/bar/(abcd|baz|barista|yo)$ [NC]
RewriteRule ^ http://site/ [R=301,L]
Another one (for .htaccess, as initial / is removed from RewriteRule)
RewriteEngine On
RewriteRule !^bar/(abcd|baz|barista|yo)$ http://site/ [R=301,L,NC]
And solution by RedirectMatch
RedirectMatch permanent ^(?!/bar/(abcd|baz|barista|yo)$).* http://site/
All work perfectly, the problem that you might have on a testing/debugging state is that browser caches 301 response. So, when you are trying to check or to write the correct code - use 302 response, not 301. And remove NC flag if case insensitivity is not required.
See this one
^\/.*(?<!foo\/bar\/(aaa|bbb|ccc|ddd))$
Match / followed by anything, unless the end of string is preceded by /foo/bar/(aaaa|bbb...)
The (?<! is a negative lookbehind attached to end of string $ which will check its impossible to match what's inside just before the end of string.
If your real case is not as static as your exemple, give real datas for the query part.

I need to find the proper 301 redirect

I'm having trouble with a 301 redirect. I've tried a few but they are not working as I need it.
I have an old website that no longer exists, I would like to forward the entire domain to a landing page on my new website.
So when someone types in www.olddomain.com I want them to go to www.newdomain.com/landingpage.php
The problem is that I want ALL requests on the old domain to go to that landing page. Currently I have the domain forwarded thru GoDaddy, and when someone (or Google) types in www.olddomain.com/whateverpage.htm or forwards it to www.newdomain.com/landingpage.php/whateverpage.htm
This is causing issues with Google indexing.
So I need a redirect that will send ALL requests of ANY page at www.olddomain.com/ to www.newdomain.com/landingpage.php
What should I use?
You can use this rule on DocumentRoot of old site:
RewriteEngine On
RewriteRule ^ http://newdomain.com/landingpage.php? [R=301,L]
Assuming you're using a LAMP stack (linux server, MySQL, PHP) you can use rewrite rules in an .htaccess file to capture all requests to www.newdomain.com/landingpage.php* and redirect them back to landingpage.php. If you're hosting on a Windows server, you can do these sort of redirect rules with the URL Rewrite module in IIS (which can import .htaccess rules).

Forcing www on all URLs without htaccess

I have a complex setup wherein am having Django 3 sub-projects kind of thing hosted on single Apache via mod wsgi.
I want to have www prepended on all addresses for one of these as some payment gateways are behaving improper without www URLs. Now, .htaccess's canonical redirect messes up the other 2. Using PREPEND_WWW from common middleware works only for foo.jp and not for foo.jp/bar/* and so on...
Similarly, I also tried to create an index.php, pointed to it from sites-enabled in apache and used header redirection to www.foo.jp for foo.jp...
Please tell me a way for all URLs i.e. anything foo.jp/, foo.jp/bar/ etc. to www.foo.jp/, www.foo.jp/bar/ and so on...
Regards !
I figured this out...
In the apache/sites-enabled/000-default
Basically, the default apache virtual host file which is read before other domain's virtual host files I added this:
<VirtualHost *:80>
ServerName playism.jp
Redirect permanent / http://www.foo.jp/
</VirtualHost>
Now, it is redirecting for everything... without the .htaccess which was messing it up !

Hosting a wordpress blog parallel with a django app

I would like to host a blog at a subfolder of my domain, which is covered by a django app. I'm most of the way there, but I need some help getting over the finish line, as it were.
Currently, if I go to domain.com, the django app is served correctly. If I go to domain.com/blog/, the blog is served correctly. However, if I go to domain.com/blog (note the missing trailing slash), the urlconf returns a URL not found error.
I've tried a couple of things, including:
Reordering the Alias, Directory, and WSGIScriptAlias statements in my Apache configuration
Having the django urlconf trap the domain.com/blog condition and redirect to domain.com/blog/ (probably unsurprisingly causing an infinite loop of redirects)
What are my next steps?
Here is the relevant part of my Apache conf:
Alias /blog/ /var/www/blog/
<Directory /var/www/blog/>
AllowOverride All
Order deny,allow
Allow from all
</Directory>
I haven't used Apache in years, but try aliasing just /blog instead of /blog/. The problem currently is that Apache is not catching it, so it's being passed to Django. If that doesn't work, you might also try setting up a 301 redirect in your Apache conf to redirect to the slash version, thereby avoiding Django altogether.