Force or enable www in front of URL

OS type and version Ubuntu Server 22.04
Virtualmin version 7.1-1

Hopefully a simple question and answer, how do I force or enable www in front of my URL? As in at the moment only works, does not and I would prefer
Thanking you.

Assuming you are using Virtualmin with Apache (and not Nginx), add to your .htaccess

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

Thank you, calport, is this the .htacces file in the document_root or in my website content subfolder, i.e. gmcomputers?

Yes, that’s right. The one in document_root.

In your case, /home/gmcomputers/public_html

Ah Ok, just a concern, will it affect my existing redirect to my gmcomputers subfolder in any way?

Yes, you need to be careful if there are other redirects etc. in play. I am assuming defaults / standard settings for the code that I have provided.

Yes there is another redirect in play:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]

So apologies for the newbie query, where exactly would I place your code? I copied and pasted the existing code from another website and while I do understand what it does, when it comes to editing, I am useless, LOL

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.