Skip to content
Linux Running · 6 min read

Apache: Redirect /blog to a Subdomain When the Site Is a Reverse-Proxied Node App

My mail signature linked to example.com/blog and got a 404. Fix: ProxyPassMatch exclusion plus a path-exact RedirectMatch 301 to the blog subdomain.

My email signature has a link to example.com/blog. Someone opening it got a 404.

The reason is boring. The blog moved to its own subdomain, blog.example.com, when I moved everything to a new domain. The main site is a Next.js app running on Node.js, and Apache sits in front of it with ProxyPass / http://127.0.0.1:3020/. Apache forwards every path to the app, the app has no /blog route, the app says 404. Every mail with that signature carried a dead link.

The fix is three lines in the Apache vhost. The app doesn't get touched.

TL;DR: exclude the path from proxying with ProxyPassMatch ... !, placed before the catch-all ProxyPass /, then redirect it with RedirectMatch 301 and pass the rest of the path through, so /blog/some-post/ lands on blog.example.com/some-post/. Language prefixes /de/blog and /en/blog included.

The fix

# /blog (signature link) -> blog.example.com, path-exact
ProxyPassMatch "^/(de/|en/)?blog(/.*)?$" !
RedirectMatch 301 "^/(?:de/|en/)?blog(/.*)?$" "https://blog.example.com$1"

Where it goes matters. My vhost directives look like this afterwards, shortened:

ProxyRequests Off
ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
ProxyPass /.well-known/ !

# /blog (signature link) -> blog.example.com, path-exact
ProxyPassMatch "^/(de/|en/)?blog(/.*)?$" !
RedirectMatch 301 "^/(?:de/|en/)?blog(/.*)?$" "https://blog.example.com$1"

ProxyPass /tools http://127.0.0.1:8130/tools
ProxyPassReverse /tools http://127.0.0.1:8130/tools
ProxyPass / http://127.0.0.1:3020/
ProxyPassReverse / http://127.0.0.1:3020/

The exclusion has to come before any ProxyPass that would also match the path. In my case that's the catch-all /. Apache checks ProxyPass/ProxyPassMatch rules in the order they appear in the config, and the first match wins. A ! rule after ProxyPass / is never reached.

Note the ProxyPass /.well-known/ ! already in there. Same trick, used so Let's Encrypt challenges are answered by Apache instead of the app. If you have that line, you already know the pattern.

Apply it without breaking the site

The site runs on KeyHelp, which manages the vhost files. Custom directives live in a per-domain include under /etc/apache2/keyhelp/custom_vhosts/. Backup first, edit, test, reload:

f=/etc/apache2/keyhelp/custom_vhosts/<user>_example.com_https.conf
cp "$f" /root/backups/example.com_https.conf.bak-blog-$(date +%Y%m%d)
# edit $f, add the three lines above the first ProxyPass
apache2ctl configtest && systemctl reload apache2

configtest before reload, always. If the syntax is broken, && stops there and the running Apache keeps serving the old config. reload instead of restart means open connections aren't dropped.

Gotcha: the panel has its own copy of your directives

KeyHelp doesn't only keep the custom directives in that file. It also stores them in its own database, and the panel works from that copy when it regenerates the vhosts. If you edit only the file, the next regeneration may quietly put the old version back. I have a small helper script that writes the same directives into both places, the panel's database and the file. The simpler way: paste the lines into the custom directives field in the KeyHelp UI and let it write the file. Either way, don't trust a hand edit to the generated file alone. More on how KeyHelp works in general in my KeyHelp overview.

Test the matrix

Don't just curl /blog and call it done. Test the variants and test that you didn't break anything else:

for u in /blog /blog/ /de/blog /en/blog /blog/some-post/ /de /tools/; do
  echo "$u -> $(curl -s -o /dev/null -w '%{http_code} %{redirect_url}' https://example.com$u)"
done
curl -sL -o /dev/null -w "final %{http_code} %{url_effective}\n" https://example.com/blog

What I got, with my domains swapped for example ones:

/blog -> 301 https://blog.example.com/
/blog/ -> 301 https://blog.example.com/
/de/blog -> 301 https://blog.example.com/
/en/blog -> 301 https://blog.example.com/
/blog/some-post/ -> 301 https://blog.example.com/some-post/
/de -> 200
/tools/ -> 301 https://example.com/toolbox
final 200 https://blog.example.com/

The first five are the new redirect. /de with 200 proves the app still serves the normal pages. The /tools/ line is a redirect the tools backend does itself, and it still does it, which proves the other proxied paths are still going to the right backend. The last line follows the chain to the end and makes sure the target answers 200, not a redirect loop or another 404.

%{redirect_url} is the nice part of that curl call: you see where the 301 points without following it.

Why the exclusion is needed at all

You'd think RedirectMatch alone would do it. It's in the config, the path matches, Apache should redirect. As far as I know that doesn't work when a ProxyPass / covers the same path: the request ends up at the app anyway. I didn't try it without the exclusion, I put the ! line in from the start.

As far as I understand the internals, ProxyPass mappings are applied by mod_proxy in Apache's URL translation phase, and mod_proxy hooks in there early, before mod_alias (where Redirect and RedirectMatch live) gets its turn. Once mod_proxy has claimed the URL, the request is a proxy request. Order in the config file doesn't change that, because the two modules aren't competing in one list. I wouldn't bet on every detail of the hook order, but the practical rule is simple: if a path is covered by ProxyPass, exclude it with ! before you try to handle it any other way. That goes for Alias, RedirectMatch and static files alike.

Gotcha: (?:...) vs (...) and what lands in $1

Look at the two regexes closely. They're almost the same:

ProxyPassMatch "^/(de/|en/)?blog(/.*)?$" !
RedirectMatch 301 "^/(?:de/|en/)?blog(/.*)?$" "https://blog.example.com$1"

In RedirectMatch, $1 refers to the first capture group. With a plain (de/|en/) group up front, $1 would be de/ or empty, and /de/blog/some-post/ would redirect to https://blog.example.comde/. Broken host, and the post path lost on top.

(?:de/|en/) is a non-capturing group: it groups for the ? but doesn't count as a capture. So $1 is (/.*), the rest of the path after blog. That's what makes the redirect path-exact: /blog/some-post/ goes to blog.example.com/some-post/, which only works because the blog kept its slugs when it moved.

In the ProxyPassMatch ... ! line it doesn't matter, nothing gets substituted there. I left it as a capturing group and it works fine either way.

Also note the anchors. ^/...blog(/.*)?$ matches /blog, /blog/ and /blog/anything, but not /blogroll or /my-blog. Without the $ and the optional slash group, /blogroll would be redirected too.

301 means "moved permanently". 302 means "temporarily somewhere else". For a link in a mail signature it's the difference between "this is where the blog lives now" and "for now, look over there".

I went with 301 because the blog isn't coming back to /blog. Search engines treat a 301 as the signal to move the old URL's value to the new one, and if someone posted example.com/blog/some-post/ on a forum two years ago, that link now leads to the right post.

The catch: browsers cache 301s, often hard, with no expiry unless you send cache headers. If you get the target wrong and fix it later, people who already hit the bad 301 keep getting sent to the wrong place from their own cache, and you can't reach in and clear it. So when I'm not sure yet, I test with 302, check the matrix, and switch to 301 once it's right. curl doesn't cache redirects, so test with curl, not a browser tab you've been reloading for ten minutes. If you must use a browser, a private window with a fresh profile.

The real mistake wasn't the missing redirect, it was assuming that the blog move covered everything. I had set up path-exact 301s from the old blog domain, but /blog on the main site was never a real path in this app, it only existed in my signature and therefore in every mail I'd sent with it. Those links are out there forever. You can't edit sent mail.

What I do now when something moves:

  • grep my own stuff for the old URL: signature, social profiles, README files, invoice templates, the footer of the old site
  • add a redirect for every URL that was ever handed out, even if it never worked as a real page
  • keep the redirects. Don't remove them after a year because "nobody uses that anymore". You don't know that, and a 301 costs nothing

And if your main site is a single-page or Node app behind Apache, remember that Apache isn't really serving anything anymore. Every path goes to the app unless you take it out first. Same thinking as in running Docker apps behind an Apache reverse proxy and the SPA fallback for static builds: decide per path who answers, Apache or the app, and write it down in the vhost in that order.


Related posts