Friday, April 8, 2016

apache 2.2 - vhost_alias_module domain.com doesn't go to www.domain.com



I'm using the configuration below to use vhost_alias_module to more easily manage my hosted setup. I want sites to be structured like the following /sites/domain.com/w/w/w/ which with the configuration below works fine. However, if I attempt to access domain.com instead of www.domain.com this falls apart. How do I fix this?



Update: I want request for domain.com to automatically be directed to www.domain.com. It would be fine if this required a 403 redirect (I think that would be preferred from a SEO perspective) .



LoadModule vhost_alias_module /usr/lib/apache2/modules/mod_vhost_alias.so


ServerAdmin admin@gmail.com

DocumentRoot /sites/

Options FollowSymLinks
AllowOverride None

;
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All

Order allow,deny
allow from all

UseCanonicalName Off
VirtualDocumentRoot /sites/%2+/%1.1/%1.2/%1.3+



Answer



Well, the requirement to imply www comes close to pushing you out of being able to use mod_vhost_alias, and into mod_rewrite territory.. but there's a couple options that I can think of:





  1. Just redirect to www.



    RewriteEngine on
    # Match anything with a two-part name
    # (will not work for 3-part base domains that would need to redirect to www, be careful)
    RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)$
    # Redirect
    RewriteRule ^/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]


  2. Let those requests work without a redirect, and use symlink trickery to stick them to the www location.



    With your VirtualDocumentRoot, requests to domain.com go to /sites/com/d/o/m - let's change that:



    VirtualDocumentRoot /sites/%-2+/%-3.1/%-3.2/%-3.3+


    This will make a request for domain.com go to /sites/domain.com/_/_/_ - make this location a symlink to /sites/domain.com/w/w/w.




No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...