【问题标题】:Laravel automatic redirect url from http to httpsLaravel 自动将 url 从 http 重定向到 https
【发布时间】:2017-03-17 02:37:14
【问题描述】:

我想为我的 Laravel 应用程序自动从 http 重定向到 https。

网址是app1.domain.com

我在公用文件夹的 .htaccess 文件中添加了以下几行,但似乎没有生效。

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

当我输入 http://app1.domain.com 时,该 url 不会被重定向并停留在 http。当我单击通过http://app1.domain.com/auth/login 访问我的应用程序时,我得到一个404。如果我手动将https 添加到https://app1.domain.com/auth/login,那么它可以工作,但如果可以自动重定向到https,我希望它。

.htaccess

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Added those 2 lines for ssl redirect
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

还尝试将我的 laravel .env 文件从 APP_URL=http://app1.domain.com 更改为 APP_URL=https://app1.domain.com,但没有做任何事情。

解决方案

使用 Web 服务器将所有 http 请求重定向到 https。

因此将我的httpd.conf 文件从:

<VirtualHost *:80>
    ServerName www.app1.domain.com
    ServerAlias app1.domain.com
    ServerAdmin email@example.com
    DocumentRoot /var/www/html/app1.domain.com/public
</VirtualHost>

到:

<VirtualHost *:80>
    ServerName www.app1.domain.com
    ServerAlias app1.domain.com
    Redirect permanent / https://app1.domain.com/  <-- this line added
    ServerAdmin email@example.com
    DocumentRoot /var/www/html/app1.domain.com/public
</VirtualHost>

别忘了service httpd restart

有什么想法吗?谢谢

【问题讨论】:

  • 托管在哪里,不是使用 nginx 吗?
  • 托管在 vps 上,使用 Apache
  • 好的,你有RewriteEngine on
  • 您是否检查过其他 htaccess 重定向是否有效。比如 301 之类的,看看是否还有其他潜在问题?
  • 是的,.htaccess 文件已经存在,我只是添加了上面的两行。我会发布它

标签: .htaccess laravel laravel-5.2


【解决方案1】:

我使用中间件功能,并将其应用于所有路由。然后,如果您需要任何特定的 url,您可以将其取下

namespace App\Http\Middleware;

use Closure;

class HttpsProtocol {

public function handle($request, Closure $next)
{
        if (!$request->secure() && env('APP_ENV') === 'production') {
            return redirect()->secure($request->getRequestUri(), 301);
        }

        return $next($request); 
   }
}

然后添加到您的 kernel.php 文件中

    'https' => [
        'App\Http\Middleware\HttpsProtocol'   
    ],

然后像这样将它添加到您的路线上

 Route::get('/', ['middleware' => array('web','https'),'uses' => 'HomeController@index']);

或全局注册

Docs

【讨论】:

  • 在 Laravel 之外执行此操作不是更好的做法吗?您这样做是因为使用 .htaccess 文件对您不起作用吗?
  • 我确实很难让 htaccess 在所有情况下都能可靠地工作,而无需编写一堆条件。您可能会争辩说 htaccess 更干净,但它们都在重定向请求,并且在 laravel 中执行此操作允许您稍后在需要时扩展它。我确信这两种方法都有争论。但这很有效,而且更容易扩展。
  • 感谢您的意见@severian,非常感谢。我找到了一个似乎可以正常工作的解决方案(使用 Web 服务器将所有 http 请求重定向到 https)。见上面的解决方案
【解决方案2】:

把它放在你的 .htaccess 文件中

RewriteEngine On

# Redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

它对我有用。

【讨论】:

    猜你喜欢
    • 2019-02-12
    • 2023-03-24
    • 2018-02-02
    • 2015-11-02
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2018-04-17
    相关资源
    最近更新 更多