Some website owners prefer to use the www prefix for their domain name, while others like to keep it simple and use the non-www version. Either way, it’s important to choose one and stick to it, as having both versions can cause duplicate content issues, and confuse your visitors.

Using Nginx to Force Redirects

One way to redirect your website to either the www or non-www version using Nginx is to edit the configuration file in your web server.

The configuration file is located in /etc/nginx-rc/extra.d and you can access it using a command-line editor such as vim or nano. You can also use RunCloud’s Nginx Config menu to create a new rule.

In the web settings section in the sidebar, click on the “NGINX Config” and then “Add a New Config“.

You can now insert the following code snippets, depending on your preference. To redirect your website to the www version, use this code:

if ($host !~ ^www\.) {
  return 301 $scheme://www.$host$request_uri;
}

This code checks if the requested domain name does not start with www. If so, it redirects it to the www version with a 301 permanent redirect.

To redirect your website to the non-www version, use this code:

if ($host ~* ^www\.(.*)) {
    set $host_without_www $1;
    return 301 $scheme://$host_without_www$request_uri;
}

This code checks if the requested domain name starts with www. If so, it redirects it to the non-www version with a 301 permanent redirect. After inserting the code, save and close the file.

You will then need to restart your Nginx server. To do this, go to the Services tab of your server, and click the “Restart” button next to Nginx.

You will also need to add a CNAME record in your DNS settings with the name of ‘www‘ and a hostname of ‘@‘. This will ensure that your website works properly with both versions.

Using Apache .htaccess to Force Redirects

Another way to redirect your website to either a www or non-www version is to use the .htaccess file in your Apache web server.

The .htaccess file is a configuration file that allows you to modify the behavior of your web server, and apply rules for your website.

To create or edit the .htaccess file, you can use the File Manager in RunCloud. Just type ‘.htaccess’ in the file name field, and click on Create File or Edit File. Then, you can insert the following code snippets, depending on your preference.

To redirect your website to the www version, use this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.\*)$ http://www.example.com/$1 [L,R=301]

This code checks if the requested domain name does not start with www. If so, it redirects it to the www version with a 301 permanent redirect.

To redirect your website to the non-www version, use this code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.\*)$ http://example.com/$1 [L,R=301]

This code checks if the requested domain name starts with www. If so, it redirects it to the non-www version with a 301 permanent redirect.

Make sure to replace example.com with your actual domain name in both cases.

Also, if you are using HTTPS for your website, change http to https in the rewrite rules.

Save the .htaccess file and test your website. It should now redirect to either www or non-www version, depending on your choice.