Reputation: 412
If apache was presented with the domain name origin.datingasia.co would it match both below VirtualHost entries?
<VirtualHost *:80>
ServerName datingasia.co
ServerAlias www.datingasia.co
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/datingasia.co
ServerName origin.datingasia.co
ServerAlias origin.datingasia.co
</VirtualHost>
Also once a domain has the rewrite rule applied - eg: has 'http://www.etc.etct' added to it does it redirect automatically? would this occur before a DocumentRoot path would be used?
Upvotes: 0
Views: 171
Reputation: 2006
Only the second entry would be triggered for the domain "origin.datingasia.co". If you wanted the first one to trigger it as well, you would need to add a second ServerAlias parameter.
ServerAlias origin.datingasia.co
This would cause a problem though since your second entry contains that same ServerAlias. All ServerName/ServerAlias must be unique, otherwise Apache will not know which block to use for the request.
The first entry will only catch requests for "datingasia.co" and "www.datingasia.co". You're missing the DocuemntRoot though, in the event that the rewrite rule condition is not met (IE: www.datingasia.co). This will cause your requests to fail once they get to the www.datingasia.co page because Apache will not know what root to serve the requests from. The browser will automatically be redirected to "www.datingasia.co" when they visit "datingasia.co".
For the second entry, you do not need "ServerAlias origin.datingasia.co" since you already have that domain defined in the ServerName. You would only need a ServerAlias line if you wanted an additional unique domain to point to this host (IE: ServerAlias www.origin.datingasia.co).
Since your rewrite rules are in the host, if the condition is met Apache will not need the DocumentRoot. But if the condition is not met, Apache will attempt to serve the request and as a result will require the DocumentRoot. Your rule will forward "datingasia.co" to "www.datingasia.co" but will fail at "www.datingasia.co" because it is missing the DocumentRoot and does not meet the RewriteCond.
Upvotes: 1