Node.js on Uberspace: Redirect non-WWW to WWW-URLs.

Learn how to redirect non-WWW requests to WWW-URLs on uberspace when using Node.js.

When connecting a custom Domain in Uberspace, you have to manually add your WWW subdomain. This way people can reach your website both with and without the WWW.

You can add both domains using the following commands:

# first add your non-WWW domain
$ uberspace web domain add yourdomain.com

# and than your WWW domain
$ uberspace web domain add www.yourdomain.com

When you run the commands, you’ll get back an A and AAAA record. Add both records to your DNS zone – you’ll end up with 4 records: the A and AAAA record pointing to your root domain and the A and AAAA for the WWW subdomain.

When that’s all set, you’re site is reachable under the WWW and non-WWW domains.

What’s the deal?

When you have two domains pointing to the same website, it can be, that Google and other search engines will think, these are two different websites.

This can mess up the stats in the Google search console and might cause Google to not index certain pages, because it thinks you have duplicate pages.

Redirecting the non-WWW domain to the WWW domain with a 301 status code will prevent that from happening.

Note: You could also do it the other way around and use the non-WWW domain as your primary domain. But that has a bad effect on performance and security.

.htaccess doesn’t work for Node.js apps in Uberspace

In Uberspace you can add a .htaccess file, that lets you define URL rewrites.

However, .htaccess won’t work, if you serve your website using anything other than the apache server.

So, if you’re using Node.js – like I did for the Astro site I was trying to setup when encountering this issue – .htaccess files wont work.

You could technically handle requests in Node.js and perform your rewrites like that. But that was not an option for me, since I was using Astro with the Node.js adapter in standalone mode – and I wasn’t keen on writing a full blown custom Express app only to handle the rewrites.

What to do instead

Now: Just because we want our website to be handled by Node.js doesn’t mean we need to handle all requests by Node.js.

That means: Using the uberspace web backend command, we can specify that we want requests to the non-WWW domain to be handled by apache.

Like so:

$ uberspace web backend set yourdomain.com --apache

This way, you can add the .htaccess file in the DocumentRoot and it works:

RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]

That’s it

I hope this helped you. If you have any inputs regarding this post, shoot me an email at hallo@timonforrer.ch.

This post was first published on and last modified on .