Setting up an alias

Updated on 28 Dec 2018

Enabling mod_alias

mod_alias should already been enabled by default, but if not it is a simple process.

sudo a2enmod alias

creating an alias

In my Yii virtual host, I want to establish some aliases for my yii projects so that I can navigate to them like this:

http://yii/yii1
http://yii/yii3

It is interesting to note that yii1, yii3 are the project folder/name and the webroot (web) resides inside it. So my alias will need to do something like this…

  • yii/yii1 -> /var/www/yii/yii1/web

I can accomplish that with the following.

alias /yii1 "/var/www/yii/yii1/web"
<Directory "/var/www/yii/yii1/web">
    Require all granted

    #this is necessary for mod_rewrite -> clean URL path for Yii without having to use index.php in the path.
    AllowOverride All 		
</Directory>
	
alias /yii2 "/var/www/yii/yii2/web"
<Directory "/var/www/yii/yii2/web">
    Require all granted
    AllowOverride All 
</Directory>

And now I can actually navigate to my project like this

Other Notes

Another use for aliases can be with web applications (e.g. phpMyAdmin) or different versions of your own web application. So instead of navigating to localhost/phpMyAdmin-4.9.4 I can link directly to it via the alias localhost/phpMyAdmin.

alias /phpmyadmin "/var/www/phpMyAdmin-4.9.4"
<Directory "/var/www/phpMyAdmin-4.9.4">
    Require all granted
    AllowOverride All
</Directory>

The benefits of this are that the directory structure and the URL can be different. In this example the directory structure clearly labels the version of phpMyAdmin I am working with, but I don’t need to know this for the URL; which makes it easier to navigate to.

Upgrading

  • When it comes time to upgrade phpMyAdmin I can download the latest release and put it in its own directory
  • Update the alias to point to the new version
  • Restart Apache so that the configuration changes take effect.

If the new version is broken, or I don’t like it or I just want to go back to the old version I just need to update the alias.

Some points

  • The same technique can be used when you upgrade a web application and you need to provide for a roll back mechanism.
  • The directory that the alias points to can reside outside your web root folder