Most websites these days a non-www (or “naked”) domain. Meaning, the website’s URL in the address bar is simply example.com
instead of using the “subdomain” of www.example.com
Either way though, we need to set a redirect, so that if a user types in the “incorrect” version, it will redirect to the preferred one. If we don’t set the redirect, then both the www.
and the non-www version would work (in some cases). But that’s not ideal.
How to Redirect to the Primary/Preferred Domain
Make sure the domains you are redirecting (both to and from) have been added to your app in ServerPilot.
For example, if you redirect from www.
as shown below, both example.com
and www.example.com
must be added in the Domains tab of your app.
Once you’ve ensured that both domains are present, you will need to access and edit the app’s .htaccess
file.
Connect via SFTP (Filezilla)
- Open Filezilla
- File > Site Manager > New Site
- Enter a name for your site, so you can save it for later use
- To connect to the app via SFTP, you need to know the Host, User, and Password
- Select your app within ServerPilot by logging in and going to Apps
- Copy the IP address of the app’s Server and add that as the Host within Filezilla (e.g.,
XXX.XXX.XXX.XXX
) - Set the Logon Type to be Normal within Filezilla
- Copy the System User and add that as the User within Filezilla (e.g.,
serverpilot
) - Set the Protocol to be SFTP within Filezilla
- Open your LastPass Vault, and find the site you’re looking for
- If it’s not there, go back to ServerPilot and click the System User name within your app. This will allow you to reset the password.
- Enter the password from LastPass into the password area for the user in your ServerPilot app.
- Paste the password into the Password field in Filezilla
- Click Connect.
- If connection fails, then something wasn’t set right within Filezilla. Most likely the password. You may need to update the password of the SFTP user within the ServerPilot app.
Access, Edit, and Overwrite the .htaccess file
- Upon successfully logging into the sFTP area of the app user…
- Within Filezilla, go to to
apps > yourinstallname > public
- Drag the
.htaccess
file to your desktop or some other area on your PC - Open it with a text editor (preferably Sublime Text)
- Find the correct rule below, depending on your needs, and copy it to your clipboard
- Paste the rule at the top* of the
.htaccess
file - Save the file
- Drag the
.htaccess
file from your PC back into the sFTP directory to overwrite the old file- Don’t drag/drop it over another folder, or it will go into that folder
- The new file is now being used by the website, so test it out!
Example: Removing www. from the Domain
The following rule within the .htaccess
file will redirect requests for www.example.com
to the same path at the domain example.com
.
#www to non-www RewriteEngine on RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Be sure to place these rules above* the WordPress rules.
Example: Forcing Redirection to www.
The following .htaccess
file will redirect requests for example.com
to the same path at the domain www.example.com
#non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} !="" RewriteCond %{HTTP_HOST} !^www. [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Be sure to place these rules above* the WordPress rules.
Notes
If your website is using a subdomain, such as testing.example.com
(and not the www. or the non-www), then it would still likely be recommended to use a redirect rule. In this case, you can likely make use of the 1st example near the top of this article. This way, a domain such as www.testing.example.com
would redirect to testing.example.com
But if your install is using a subdomain, please ensure you test the redirect fully to make sure it works properly.
* Rule placement
By default, WordPress adds its own set of rules to the .htaccess
file. Those rules usually start with # BEGIN WordPress
and end with # END WordPress
. Please place YOUR rules above all of that. In addition, if your website has WP Rocket installed, that plugin has likely added a lot of rules to the .htaccess
file too. Please add your rules above those too. Basically, add your rules to the TOP of the .htaccess
file.
Ways to Test the Redirect
For www. to non-www
http://example.com http://www.example.com https://www.example.com
Should all redirect to:
https://example.com
For non-www to www.
http://example.com http://www.example.com https://example.com
Should all redirect to:
https://www.example.com
All redirects should go to your preferred domain version, with HTTPS as the protocol. It would also be ideal to test with an image path too, to ensure the image URL redirects to the preferred HTTPS domain, even if the user were to type in the non-preferred versions. E.g., if you’re preferred domain is example.com
, then http://www.example.com/wp-content/uploads/your-image.png
should redirect to https://example.com/wp-content/uploads/your-image.png
Keep This in Mind
Also note that a 301
redirect is often “cached” by the browser. So if you are only testing, or unsure, you may end up causing your browser to cache the redirect you add. If you’re simply “testing” the redirect, you can change the 301
to be a 302
. The difference is that the 301
is a “Permanent” redirect, and is therefore cached — where as the 302
is a “Temporary” redirect is as not cached.
Last Resort
The above rules were supplied to us via ServerPilot support team.
However, if for some reason they don’t seem to do the trick, the ones below may help instead. But please only use these, if the ones above didn’t work for some reason.
Source: https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/
www. to non-www
#alternative www to non-www RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www. [NC] RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
non-www to www.
#alternative non-www to www RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www. [NC] RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC] RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]