Jump to content

Multi Language site with one domain per language


gebeer
 Share

Recommended Posts

Hello, I searched the forum but couldn't find posts about the exact same scenario that I am facing.

Instead of serving the different languages through a language specific URL, Each language should have their own domain. Everything is served from one single PW installationh, no multisite setup.

So instead of
domain.com/
domain.com/de/
domain.com/pl/
etc.

the language versions should be available under
domain.com
domain.de
domain.pl
etc.

I found how to switch the language based on the http hostname.

But there are a few more questions:

  1. How to properly implement the redirection in .htaccess
  2. How to configure PW so it doesn't append the language to the URL?  (just leave the language names out on the home page settings tab?)

I hope someone has had that scenario before and can help out with some hints. Thank you.

 

  • Like 1
Link to comment
Share on other sites

Clever idea. I've just test-implemented that on one of my multi-language sites with .de and .com domains and it works pretty smooth following these steps:

  • all DNS entries point to the same PW instance root
  • in site/config.php I've added all allowed domain names
  • in _init.php I've quickly added this (only for com, should match a field in language setting):
if(strpos($config->httpHost, ".com") > 0)
    {
    foreach($languages as $lang)
        {
        if($lang->name == 'en')
            {
            $user->language = $lang;
            break;
            }
        }
    }

That's it, now all language fields follow the selected language. Switching between languages is now switching between domain names.

Thanks for that hint!

 

  • Like 6
Link to comment
Share on other sites

Hello,

we found the following solution that is quite similar to the suggestion from Autofahrn. Our setup includes two languages
- de (german) as default for *.de domain (default language)
- en (english) for *.com domain


In _init.php there is just:

if (preg_match("/(:2284|\.com)/", $config->httpHost)) {
    $user->language = $languages->get('englisch');
}


Your question no. 2: It is important that you configure the settings for your root page to "/" for each language. In that case PW doesn't prefix your URL paths for the children.

Be careful! In case you use the front page editor - when a child page has the same path for every language, the front page editor is not able to save the page correctly. You have to use the backend. But if there are differences, for example *.com/imprint and *.de/impressum - the front page editor works fine.


 

  • Like 6
Link to comment
Share on other sites

Just verified that this can be made running in combination with @BitPoet's simple language switcher:

I've basically added a text field url_domain to the language template and moved the default language detection from _init.php into the updatePath method (so my loop makes much more sense now):

	public function updatePath($path) {
		$httpHost = $this->wire('config')->httpHost;
		$languages = $this->wire('languages');

		// Setup default language by domain root
		foreach($languages as $lang)
			{
			if(($lang->url_domain != "")
			&& (strpos($httpHost, $lang->url_domain) > 0))
				{
				$this->user->language = $lang;
				break;
				}
			}

This change eliminates most of the other default processing, since the default already set based on the domain name.

Throughout the site I'm using localUrl to generate internal link urls, that code was changed to get rid of the language segment in case it is the default for the current domain:

	public function hookPageLocalUrl(HookEvent $event)
		{
		$lang = $this->getLanguage($event->arguments(0));
		$config = $this->wire('config');
		if(($lang->url_domain != "")
		&& (strpos($config->httpHost, $lang->url_domain) > 0))	// Default for this domain?
			$event->return = $config->urls->root . ltrim($event->object->path, "/");
		else
			$event->return = $config->urls->root . ltrim($lang->name . $event->object->path, "/");
		}

The benefit of this is, that languages can be added to the site without the need having an associated domain for each. And it does not break old links on my site, which still have the language in the url. So www.domain.com/pagename and www.domain.de/en/pagename still retrieve the same content and another language may be reached through www.domain.de/fr/pagename or www.domain.com/fr/pagename for example.

Edit: Just in case someone wants to see this in action:

www.versus-x.com (english)
www.versus-x.de (german)
www.versus-x.com/de/ (also german)
www.versus-x.com/fr/ (french, incomplete, for testing only)
www.versus-x.de/fr/ (french, only parts as well)

  • Like 3
Link to comment
Share on other sites

  • 7 months later...
On 1/16/2019 at 1:45 PM, Jan P. said:

Hello,

we found the following solution that is quite similar to the suggestion from Autofahrn. Our setup includes two languages
- de (german) as default for *.de domain (default language)
- en (english) for *.com domain


In _init.php there is just:


if (preg_match("/(:2284|\.com)/", $config->httpHost)) {
    $user->language = $languages->get('englisch');
}


Your question no. 2: It is important that you configure the settings for your root page to "/" for each language. In that case PW doesn't prefix your URL paths for the children.

Be careful! In case you use the front page editor - when a child page has the same path for every language, the front page editor is not able to save the page correctly. You have to use the backend. But if there are differences, for example *.com/imprint and *.de/impressum - the front page editor works fine.


 

This works beautifully, thanks.

& if any users need help on configuring their DNS (to point 2 domains to a single install of PW) on WHM use the >cPanel > Domains > Aliases function to add your extra domains (after enabling on > Server Configuration > Tweak Settings > Domains > "Allow users to park subdomains of the server’s hostname".

Link to comment
Share on other sites

  • 7 months later...
On 1/18/2019 at 1:36 AM, Autofahrn said:

The benefit of this is, that languages can be added to the site without the need having an associated domain for each. And it does not break old links on my site, which still have the language in the url. So www.domain.com/pagename and www.domain.de/en/pagename still retrieve the same content and another language may be reached through www.domain.de/fr/pagename or www.domain.com/fr/pagename for example.

Wouldn't you want to not have the same content available multiple times? Shouldn't you rather redirect the old URLs to the new domain?

Link to comment
Share on other sites

1 hour ago, uiui said:

Wouldn't you want to not have the same content available multiple times?

At least in my setups I prefer to see the same content, regardless if I come from the international domain (i.e. www.domain.com/pagename) or use the language sub-path (www.domain.de/en/pagename).

1 hour ago, uiui said:

Shouldn't you rather redirect the old URLs to the new domain?

Sure you could, with this setup its just not necessary.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...