Laravel:如何强制HTTPS?

时间:2017-05-16 15:52:51

标签: laravel .htaccess mod-rewrite https laravel-5.4

我开始开发一个新的大应用程序,这次我正在使用Laravel,这是第一次。

我需要为所有页面强制使用HTTPS,但是从代码或.htaccess开始,这并不重要,但我找不到简单的教程。

官方文档没有谈论这个问题。

有关信息,我的实际.htaccess是

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

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

我的问题是Laravel 5特有的,因为我不知道修改这个.htaccess文件的位置和方式。而且我也问你这是否是Laravel的正确方法,或者Laravel是否有特定的设置来处理HTTP。

所以请不要关闭我的问题,并尝试更加坚持Laravel特定主题。

如果您可以发布一种简单的方法来修改此文件和/或在Laravel配置中修改哪些内容以正确处理https。

但是,简而言之,我想强制每次通话都通过HTTPS传输。

12 个答案:

答案 0 :(得分:15)

您需要将其添加到.htaccess文件中:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://YOURWEBSITEDOMAIN/$1 [R,L]

看到这个: http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file

答案 1 :(得分:9)

尝试在.htaccess文件中添加此代码。

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

答案 2 :(得分:4)

当您要使用https渲染所有URL时。最简单的方法是 在app / Providers / AppServiceProvider.php的 boot()函数中使用以下代码:

\ URL :: forceScheme('https');

答案 3 :(得分:1)

这对我有用

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.*)$ public/$1 [L]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} !^public [L,R=301]
</IfModule>

答案 4 :(得分:1)

更改您的文件.htaccess

在以下位置控制自己的统治地位

RewriteCond%{HTTP_HOST} mydomain.com [NC]

RewriteRule ^(。*)$ https://mydomain/ $ 1 [R,L]

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

  RewriteEngine On

  # Added to Force HTTPS
  RewriteCond %{HTTP_HOST} mydomain\.com [NC]
  RewriteCond %{SERVER_PORT} 80
  RewriteRule ^(.*)$ https://mydomain/$1 [R,L]

  # Redirect Trailing Slashes If Not A Folder...
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)/$ /$1 [L,R=301]

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

  # Handle Authorization Header
  RewriteCond %{HTTP:Authorization} .
  RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

答案 5 :(得分:1)

将此添加到AppServiceProvider中的启动方法

if($this->app->environment('production'))
{
   $this->app['request']->server->set('HTTPS','on');
}

答案 6 :(得分:0)

你可以先试试这里。关于同一问题的答案有很多问题。

https://stackoverflow.com/a/4399158/5892849

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

答案 7 :(得分:0)

尝试更改.env文件中的“ APP_URL

APP_URL = http://your-domain.com

APP_URL = https://your-domain.com

答案 8 :(得分:0)

这对我有用:

# Check if HTTPS is enabled
#RewriteCond %{HTTPS} ^on$ [NC]
#RewriteCond %{QUERY_STRING} !https-is-on [NC]
#RewriteRule (.*) /?https-is-on [R,L]

# Redirect all HTTP to HTTPS requests
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

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

# Remove index.php from the url
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

答案 9 :(得分:0)

在 htaccess 文件中传递这段代码

<IfModule mod_rewrite.c>
    RewriteEngine On        
    RewriteCond %{HTTPS} !=on    
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

答案 10 :(得分:0)

此 .htaccess 将从您的 URL 中删除 /public/强制使用 https://

将此 .htaccess 放在您的根文件夹中,而无需将 server.php 文件重命名为 index.php,此 .htaccess 将完成所有操作

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

    RewriteEngine On
    RewriteCond %{HTTPS} !=on    
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteRule ^(.*)$ public/$1 [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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

</IfModule>

答案 11 :(得分:-1)

这是我的配置,以及该主题中的其他配置,我进行了循环重定向。

(也将www重定向到没有www的地方)

RewriteEngine On



RewriteBase /
#redirect from www to non-www(https)
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

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

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]