2

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

keisar
  • 5,266
  • 5
  • 28
  • 28

2 Answers2

7

I would use this line in config.php...

define('YOURLS_SITE', 'http://' . $_SERVER['HTTP_HOST'] . '');

(note to add any /subdirectory or whatever if that applies)

then as long as your apache hosts config is correct, any domain or subdomain pointing at this directory will work. keep in mind though, any redirect will work with any domain, so domain.one/redirect == domain.two/redirect

nrutas
  • 4,983
  • 2
  • 16
  • 16
  • That's a great idea if you have the same configuration for all domains. In my case I had different configuration (enable/disable SSL) for each domain, so I had to check which domain is used – keisar Mar 28 '15 at 08:00
2

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

keisar
  • 5,266
  • 5
  • 28
  • 28