2

Let's say I have a web server with IP address: 1.2.3.4.

I also have 2 subdomains:

 sth.city.eu
 inf.xyz.city.eu

I'm using apache 2 on Ubuntu Server. Here's my /etc/apache2/sites-enabled/site file:

<VirtualHost *:80>
    ServerName www.sth.city.eu
    ServerAlias sth.city.eu
    ServerAlias inf.xyz.city.eu
    ServerAlias www.inf.xyz.city.eu
    ServerAdmin webmaster@localhost

DocumentRoot /var/www/city/

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

<Directory /var/www/city/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

    LogLevel warn
    ErrorLog ${APACHE_LOG_DIR}/error_city.log
    CustomLog ${APACHE_LOG_DIR}/access_city.log combined
</VirtualHost>

I would like to make something like this: if someone will go to inf.xyz.city.eu, server should automatically redirect him to sth.city.eu (change the url). Is that possible - how can I do this? I've searched google and found Redirect, tried to use it like this:

Redirect http://inf.xyz.city.eu http://www.sth.city.eu

But it did not work.

mazix
  • 65
  • 1
  • 8

3 Answers3

3

I believe you can solve this by setting up 2 vhosts:

<VirtualHost *:80>
    ServerName inf.xyz.city.eu
    ServerAlias www.inf.xyz.city.eu
    Redirect / http://sth.city.eu/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.sth.city.eu
    ServerAlias sth.city.eu
    ServerAdmin webmaster@localhost      
    [...]
</VirtualHost>
Mikolan
  • 163
  • 1
  • 7
  • It did not work :( – mazix Jan 29 '15 at 20:02
  • @mazix: Can you verify that the alias module is enabled? You can check with `sudo apache2ctl -M` – Mikolan Jan 30 '15 at 08:15
  • Yes, it is enabled – mazix Jan 30 '15 at 12:26
  • @maxix, you need to remove the ServerAlias entries from your main (non-vHosts) configuration, and change the ServerName to _default_. Don't use the main configuration anymore - move it into the appropriate vHost as shown by Mikolan. – roaima Jan 31 '15 at 16:57
  • @roaima: could you please give an example of this? I just sterted with Apache and it's (at least now) a bit complicated to me). – mazix Jan 31 '15 at 17:10
  • It may look obvious, but did you restart apache after having modified the configuration ? – db_ch Jan 31 '15 at 18:16
  • The thing you're talking about is called **Canonical Hostname**, and is a common enough request that it's used as one of the [examples in the mod_rewrite documentation][1]. [1]: https://httpd.apache.org/docs/current/rewrite/remapping.html#canonicalhost – Ben Williams Feb 02 '15 at 04:23
1

If you have mod_rewrite, you can use RewriteRules:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^inf\.xyz\.city\.eu [NC]
RewriteRule (.*) http://sth.city.eu/$1 [R=301,QSA,L]
baggy
  • 21
  • 3
1

What @mikolan was saying is:

in /etc/apache2/sites-enabled/ you should have 2 vhost files:

  1. file: /etc/apache2/sites-enabled/sth.city.eu
<VirtualHost *:80>
    ServerName sth.city.eu
    ServerAlias www.sth.city.eu
    ServerAdmin webmaster@localhost      

    DocumentRoot /var/www/city/

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory /var/www/city/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    LogLevel warn
    ErrorLog ${APACHE_LOG_DIR}/sth.city.eu.error.log
    CustomLog ${APACHE_LOG_DIR}/sth.city.eu.access.log combined
</VirtualHost>
  1. file: /etc/apache2/sites-enabled/inf.xyz.city.eu
<VirtualHost *:80>
  ServerName inf.xyz.city.eu
  ServerAlias www.inf.xyz.city.eu
  Redirect / http://sth.city.eu/

  LogLevel warn
  ErrorLog ${APACHE_LOG_DIR}/inf.xyz.city.eu.error.log
  CustomLog ${APACHE_LOG_DIR}/inf.xyz.city.eu.access.log combined

</VirtualHost>

Of course, both files are created in /etc/apache2/sites-available, and are enabled with

sudo a2ensite inf.xyz.city.eu
sudo a2ensite sth.city.eu
sudo service apache2 restart
  • Every time you enable/disable site/module a reload is enough, but while testing, restart is more safe (peace mind)

  • Check if the mod_alias is enabled (should be by default)

  • Check the logs to see what errors you get: insufficient permissions, you don't even get the requests?
azbarcea
  • 123
  • 4