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


laravel example, laravel teaching, livewire example, laravel

How to set cultural error messages in Laravel Livewire components

This article uses a simple Laravel livewire example to see how the livewire component verifies user input errors and displays Chinese error messages.

laravel

Basic application teaching of jenkins CI Server pipeline on Laravel

Do you want to build a continuous integration and delivery CI Server for Laravel through docker? Execute laravel dusk and phpunit on CI Server to easily handle automated testing and related records. After reading this article, you may find out how simple CI/CD is. I mean basic use.

docker,laravel

How to use multiple YAML files to execute multiple docker container services

This video introduces how to use the docker-compose command to specify multiple YAML structure language configuration files to create container execution, and introduces how we simplify docker-compose by using .env in the D-Laravel environment Start command.