Today, many websites can operate with or without the www prefix (e.g., example.com).
In this article, we will explain step by step how to add and remove the www prefix.
One of the powerful tools used for URL redirection on Apache web servers is the .htaccess file. Now, let's address different cases:
For HTTP + www (If there's no SSL)
If your site doesn't have an SSL certificate (meaning https:// doesn't work) and you want it to appear only as http://www.example.com, add the following code to your .htaccess file to redirect only to www.
RewriteEngine On
# Redirect non-www HTTP requests to www HTTP
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
For HTTPS + www (If SSL is available)
If your site has an SSL certificate (meaning https:// works) and you want it to appear only as https://www.example.com, add the following code to your .htaccess file to redirect only to www.
RewriteEngine On
# Enforce www and HTTPS redirection
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Important Note:
In the above code, replace example.com with your own domain name.
If you are not using an SSL certificate (i.e., only accessible via http:// protocol), add the following code to your .htaccess file to remove www from URLs that include it.
RewriteEngine On
# Redirect www HTTP requests to non-www HTTP
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
If your site has an SSL certificate (i.e., accessible via https://), add the following code to remove www from URLs that include it.
RewriteEngine On
# Redirect www HTTPS requests to non-www HTTPS
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
1. Log in to Plesk
Log in to your Plesk panel.
2. Go to Websites & Domains
Click on the "Websites & Domains" tab in the left menu.
Find the domain you want to work with from the listed domains.
3. Click on Hosting & DNS
Click on Hosting & DNS under your domain name.
4. Click on Hosting
Finally, click on the Hosting option.
5. Select Preferred Domain
You will see the following field on the page:
Preferred domain*
Select your preference and click the Save button.
Congratulations! You've successfully added or removed the www prefix. You can check it by clearing your browser cache.