Fix www to Non-www Redirect for Your Discourse Forum
To add a redirect from the "www" subdomain to the non-www version of your domain in Discourse, you'll need to modify your web server configuration. Here's how you can do this:
server {
listen 80;
server_name www.yourdomain.com;
return 301 $scheme://yourdomain.com$request_uri;
}
server {
listen 80;
server_name yourdomain.com;
# Your existing Discourse configuration goes here
# ...
}
This Nginx configuration will handle the redirect from www to non-www for your Discourse installation. Here's a breakdown of what it does:
The first
server
block listens on port 80 for requests towww.yourdomain.com
.It then uses a
301
(permanent) redirect to send these requests to the non-www version of your domain.The second
server
block is where your existing Discourse configuration should go.
To implement this:
Replace
yourdomain.com
with your actual domain name in both places.Add this configuration to your Nginx configuration file (usually located at
/etc/nginx/nginx.conf
or in a separate file in/etc/nginx/sites-available/
).If you're using HTTPS (which is recommended), make sure to also set up similar redirects for port 443.
After making changes, test your Nginx configuration with
nginx -t
.If the test passes, restart Nginx to apply the changes:
sudo service nginx restart
orsudo systemctl restart nginx
.
Remember to also update your DNS settings to ensure that both the www and non-www versions of your domain point to your server.
Would you like me to explain any part of this configuration in more detail?
Subscribe to my newsletter
Read articles from Ewan Mak directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ewan Mak
Ewan Mak
Crafting seamless user experiences with a passion for headless CMS, Vercel deployments, and Cloudflare optimization. I'm a Full Stack Developer with expertise in building modern web applications that are blazing fast, secure, and scalable. Let's connect and discuss how I can help you elevate your next project!