virtualhostyourls

Use one instance of Yourls URL shortner with multiple domains


I've been looking for a way to use Yourls with multiple domains, the main issue is that when configuring Yourls you need to supply the domain name in the config.php file (YOURLS_SITE constant). Configuring just one domain with actual multiple domains pointing to Yourls causes unexpected behavior.

I've looked around and couldn't find a quick hack for this


Solution

  • I found this quick-and-dirty solution and thought it might be useful for someone.

    in the config.php file I changed the constant definition to be based on the value of $_SERVER['HTTP_HOST'], this works for me because I have a proxy before the server that sets this header, you can also define virtual hosts on your Apache server and it should work the same (perhaps you will need to use $_SERVER['SERVER_NAME'] instead).

    so in config.php I changed:

    define( 'YOURLS_SITE', 'http://domain1.com');
    

    to

    if (strpos($_SERVER['HTTP_HOST'],'domain2.com') !== false) {
            define( 'YOURLS_SITE', 'http://domain2.com/YourlsDir');
    
            /* domain2 doesn't use HTTPS */
            $_SERVER['HTTPS']='off';
    
    } else {
            define( 'YOURLS_SITE', 'https://domain1/YourlsDir');
    
            /* domain1 always uses HTTPS */
            $_SERVER['HTTPS']='on';
    
    }
    

    Note1: if Yourls is located in the html root you can remove /YourlsDir from the URL Note2: The url in YOURLS_SITE must not end with /

    Hopefully this will help anyone else