Forum Moderators: phranque
After some playing with rewrite rules, I came up with the following idea.
1. Test if HTTP_HOST matches a pattern ^part1.part2$
2. If the above is the case, replace it with www.%{HTTP_HOST}.
Because I check if the HTTP_HOST matches two parts with a central dot, I won't add www to existing sub-domains. This is the code I want to use. Any problems or things I didn't look after yet? It seems so simple and would allow me to remove a dozen or so .htaccess files.
RewriteCond %{HTTP_HOST} ^[^\.]+\.[^\.]+$
RewriteRule ^/(.*)$ h**p://www.%{HTTP_HOST}/$1 [R=301,L]
See the Context description under the documentation for each Apache module directive. This specifies the allowable contexts for each directive. For RewriteRule, the context is "server config, virtual host, directory, .htaccess". So it should work within a <Directory> container, and within a server config container.
Jim
<VirtualHost *:80>
ServerName www-redirect
ServerAlias site1.com
ServerAlias site2.com
ServerAlias ...
ServerAlias siteN.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\./]+\.[^\./]+$
RewriteRule ^/(.*)$ h**p://www.%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerName www.site1.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.site2.com
...
</VirtualHost>
...