by Devin Yang
(This article was automatically translated.)

Published - 1 year ago ( Updated - 1 year ago )

Before we start, let me complain. Originally, my website could automatically display different languages ​​according to the user's browser.
That's okay, but Google's SEO doesn't seem to like it, he recommends using different URLs for each language,
Don't use cookies or browser settings to adjust the content of the page, well then I can only be good Give in.
His suggested method,
The first solution:
Divided by country, such as example.tw or example.de
How is this really impossible , Either buy the registered domain name, or take a strange domain name that no one in the world will take.
The second solution:
Using sub-domain names to distinguish, this is what I am going to do, and so on to explain how to do it in Laravel.
The third solution:
example.com/tw/ and the like, Apple seems to be doing this.
The fourth solution:
site.com?loc=tw and the like are not recommended, indeed I think this is not a good idea.

Of course I think, just change one code, don’t change different codes for different websites,
Laravel has twin martial souls and innate full soul power, don’t you understand? , I watch too many cartoons,
I mean Laravel has a built-in multilingual environment, and even .env can have multiple ones,
Of course it is more convenient to run all the languages ​​I want to support in one host environment, right? ?

After a lot of complaining and nonsense, the question is, how do I fix it.
Open bootstrap/app.php, and paste the code below $app initialization:
Through $app->loadEnvironmentFrom, we can automatically load different .env files according to the sub-domain name, ie. env.[subdomain].

In principle, our proxy must have a defined sub-domain name to get the relative .env.[sub-domain name] file, it is impossible to mess around,
I don’t think there will be any security in theory Concerns, that's how I understand it.

<?php

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

//依子域名載入env
if(!empty($_SERVER['HTTP_HOST'])){
    $subdomain_envfile = sprintf(".env.%s",explode(".",$_SERVER['HTTP_HOST'])[0]);
    $app->loadEnvironmentFrom($subdomain_envfile);
}

In the above program, let me explain, the detection of empty($_SERVER['HTTP_HOST']) is very important.
Because artisan does not have such a thing as $_SERVER['HTTP_HOST'], this will cause failure to load .env.
So it is necessary to determine the existence of $_SEVER['HTTP_HOST'] before going to the .env.[subdomain name] file containing the subdomain name.

Suppose, I have two domain names:
www.ccc.tc and en.ccc.tc (planning to go online), don’t try it yet,
my automated wildcard subdomain The voucher application process uses up all the quota every week, and I have to wait another day before I can apply for a new @@.
Then, open en.ccc.tc, and the environment of .env.en can be loaded automatically.
If our settings are all right, tinker can also be opened normally with the --env parameter.

Secretly revealed , I have already made many calls to the translation API. There are a few who are truly Buddha-minded. They count times, not words. I can't tell this, so I can only say sorry to those manufacturers.
The structure has been completed, and I have the opportunity to expand some different APIs. I can’t care about the translation quality, and I can manually adjust it at most 😝
I have used several 0 yuan subscription plans. The idea is to make this website support Chinese, English and Japanese 😎.


If you like This article, save my SEO, give me a thumbs up, thank you.
 

Tags: laravel dotenv seo

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


dlaravel

How to automatically detect and update the certificate issued by let's encrypt through getssl

It's really great that let's encrypt provides free certificates. However, is it a bit troublesome to update the certificate every three months? This article introduces how to generate multi-domain SAN certificate settings through getssl, and schedule automatic certificate renewal. It's very simple, just six steps to get it done.

Laravel, Packages,

What packages do you have to install on all Laravel applications you build?

What kind of packages are you sure to install on your Laravel application? There are four-star kits here, almost everyone will install them, Click here to see, how do I say, my actual use experience sharing...

laravel

Let's run Laravel

If you haven't heard of D-Laravel, let me introduce it. D-Laravel is a Laravel and docker execution environment built with docker-compose. Let us quickly generate Laravel projects through simple bash. This article teaches you how to use the docker image provided by D-Laravel to build a swoole execution development environment. After reading this article, you should be able to quickly create a swoole execution environment through D-Laravel.