by Devin Yang
(This article was automatically translated.)

Published - 2 years ago ( Updated - 2 years 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

Safe Update D-Laravel

How to upgrade the current D-Laravel. If your current use environment is very smooth and you have not encountered any problems, you don't need to update it. If you want to use the latest version, and bug fixes, etc., please read this article carefully, In order to ensure a smooth upgrade, the following is the recommended way to upgrade D-Laravel.

laravel,oauth2

My most used Laravel passport directives

Laravel provides quite a lot of useful packages, such as Passport is one of them, when the website needs to set up its own OAuth2 authentication host, it really saves time and effort. Take this site as an example, because the front-end and back-end separation mechanisms are adopted (there are two different Laravel projects), and I verify them through the front-end WEB. Basically, I have many other different projects, and they all rely on OAuth2 provided by Laravel to handle cross-domain authentication.

Laravel linux

My mobile computer, install Laravel 8 on Sony xperia 5 II

This is my first Android phone. It's no problem to try to install Laravel. Basically, I installed vim and mariadb. After executing sshd, you can log in through 8022, it is completely a mobile computer: p. The software I use is Termux...