Siva
Siva

Reputation: 51

apache 2.4 name based virtualhost not working

I am using Amazon Ec2 and i have consolidated the 2 sites and need to setup different redirect rules for my old sites. but when i tried to the same with the below configuration, the web server always takes the first virtual host entry (site1.com) and not taking the second virtual host entry ( site2.com). Can you check whether i am missing anything.

Please see below my config.

httpd.conf :

Listen 0.0.0.0:80
Listen 0.0.0.0:443
Include conf/extra/httpd-vhosts-all.conf

httpd-vhosts-all.conf :

<VirtualHost *:80>
  ServerName site1.com
  ServerAlias site1.com 
  DocumentRoot "/websites/site1"  
  <Directory /websites/site1 > 
    Require all granted
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
  </Directory>    
  RewriteEngine On
  RewriteRule     ^/page1$  page2.html
</VirtualHost>
<VirtualHost *:80>
  ServerName site2.com 
  ServerAlias site2.com    
  DocumentRoot "/websites/site2" 
  <Directory /websites/site2 > 
    Require all granted
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
  </Directory> 
  RewriteEngine On
  RewriteRule     ^/page3$  page4.html
</VirtualHost>

Thanks Siva

Upvotes: 0

Views: 3660

Answers (2)

Barry Pollard
Barry Pollard

Reputation: 45870

The first vhost is used by default if no other host is matched.

So you need to figure out why you are not matching the second host.

Are you perhaps using www.site2.com? That would fail to match the second vhost (which is only set up for site2.com without the www). I also don't understand why you have the same value set up in ServerName and ServerAlias? It's usual to have the preferred ServerName set up in the first and any alias's in the second (including the www version).

So instead of:

ServerName site1.com
ServerAlias site1.com 

You would normally do:

ServerName site1.com
ServerAlias www.site1.com 

You can also get a list of currently configured vhosts using the following command:

apachectl -S

Other than that all I can suggest is looking for typos, ensuring Apache is restarted properly (but that seems to be ok since you can get it working when putting the 3->4 redirect in first instance), ensuring the host is being pased to Apache properly (e.g. If Apache is behind a proxy) and the page exists on site2 as per Brandon's suggestions and checking log files for errors.

Upvotes: 1

Brandon Harris
Brandon Harris

Reputation: 987

  1. Would definitely try clearing your browser cache as covener mentioned... if you haven't already.
  2. Make sure you can hit the intended rewrite target successfully?

http://site2.com/page4.html

  1. If neither of those reveal the issue, you can add %v and %{host}i to your active LogFormat to confirm the incoming Host header value and which ServerName Apache is using. For example:

LogFormat "%h %l %u %t \"%r\" %>s %b HOST=%{host}i ServerName=%v"

Upvotes: 2

Related Questions