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

How do I upgrade the backend to Lravel 5.5

At the beginning, my background was a framework created by myself, which also uses MVC architecture, database connections and environment configuration files made by myself, including my own template syntax, until I want to support Restful, I have an idea, why should I rewrite the same function myself after others have written it, would it be better to write it out? So I started to use the framework, At the beginning, Slim was adopted mainly because it supports a lower version of php, but because Slim's twig templates are not as easy to use as Laravel's blade template...

laravel,woops

Laravel 5.5 Woops is back, let's check it out.

Woops is a framework for PHP error handling. It was preloaded in Laravel 4, but it was removed in Laravel 5.0, and now 5.5 is back.... Some features of Whoops include: - Flexible, stack-based error handling - No dependencies are required to use the current stand-alone library - Simple API for handling exceptions, tracking frames and their data -Include a stunning error page within your web application - Includes the ability to open referenced files directly in editors and IDEs - Includes handlers for different response formats (JSON, XML, SOAP) -Easy to extend and integrate with existing libraries - Clean, well structured and proven generation

laravel,laravel livewire

Laravel livewire file upload demo

Livewire is a full-stack framework for the Laravel framework. It was the first time I wrote code, and he was probably the one who was able to write and marvel at the magic. My colleague also marveled while writing after being pushed by me. When I first learned Vue, I was not so surprised by him.