Explore. Grow & Outshine

Laravel

https trong Laravel

Chuyển từ HTTP sang HTTPS trong Laravel

Cấu hình

Đầu tiên bạn phải đảm bảo hosting/sever của bạn có hỗ trợ hoặc đã cài đặt SSL cho tên miền. Sau đó, mở tệp .env và đặt liên kết HTTPS của trang web của bạn thành APP_URL:

APP_URL=https://domain.com

Các bạn nên thể xóa cache sau khi cập nhật file .env

php artisan cache:clear
php artisan config:cache

Apache

Nếu ứng dụng của bạn đang chạy trên Apache Sever thì cập nhật file .htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Nginx

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com; 
    return 301 https://example.com$request_uri;
}
Bây giờ bạn có thể truy cập URL trang web của mình và kiểm tra xem nó có hoạt động hay không. Nếu nó không hoạt động, hãy làm theo các bước tiếp theo.

Force HTTPS for All Routes

Mở  app/Providers/AppServiceProvider.php và cập nhật boot() method:

use Illuminate\Support\Facades\URL;

public function boot()
{
    URL::forceScheme('https');
}

Bạn có thể thêm điều kiện

if (env('APP_ENV') === 'production') {
    URL::forceSchema('https');
}

if (env('APP_ENV') !== 'local') {
    URL::forceScheme('https');
}

if (env('APP_FORCE_HTTPS', false)) {
    URL::forceScheme('https');
}

Force HTTPS for Pagination Links

use Illuminate\Support\Facades\URL;

public function boot()
{
    if (env('APP_ENV') === 'production') {
        $this->app['request']->server->set('HTTPS','on'); // this line

        URL::forceSchema('https');
    }
}
Theo dõi
Thông báo của
guest
0 Góp ý
Phản hồi nội tuyến
Xem tất cả bình luận
0
Rất thích suy nghĩ của bạn, hãy bình luận.x