I have read a number of formulas for redirecting example.com
to www.example.com
and this appears to apply to .htaccess
. However, I am confused about how this might work.
I want to assume that I have no access to the Apache vhosts configuration, and that I need to do it with .htaccess
.
Suppose the configuration contains something like this:
<VirtualHost *:80>
ServerName www.example.com:80
ServerAlias www.example.com
VirtualDocumentRoot /whatever/example.com/www
</VirtualHost>
One such formula is something like this"
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ www\.%{HTTP_HOST}/$1 [R=301,L]
However, I don’t see how one root directory can respond to both requests.
The question is: Is it even possible to redirect example.com
to www.example.com
using .htaccess
only, without tweaking the Virtual Host file?
The way I see it, the original request
I run my own server, and I can do anything I like. However, I am asking this because many of my clients and students have no access to tehe configuration, and can only fiddle with .htaccess
.