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


laravel,sftp

Use Laravel's Storage SFTP Drvier for remote file upload

Why do I separate the front and back of the website? My idea is very simple, that is to rely on a set of background to control all the website data in the foreground. Assuming that the front-end website is a pure marketing website, it is nothing more than the subject content, just like the article above, without any particularly complicated logic. Therefore, it is enough to set up the backend database and connect different frontends. Then there is the last question, how can my backend HTML editor post pictures directly to the frontend? Laravel's Storage SFT Driver is a good antidote.

laravel

Customize your own helper in laravel

In the Laravel framework, a considerable number of PHP functions (php functions), called helpers, are included. https://laravel.com/docs/5.6/helpers So how do we customize our own helper in Laravel? It's actually quite simple.. Just add files in autoload in composer.json.

openapi,ai,laravel

Write OpenAI test API on Laravel

Recently, everyone seems to be talking about OpenAI. If you want to play, you can write a simple API test in Laravel's API. Since you can use curl to call, you can call through guzzle. If you log in to the openai website, you can see the official teaching instructions herehttps://beta.openai.com/docs/quickstart/build-your-applicationAPI的KEY的申請, there is a button at the bottom of this page + Create new secret key