【问题标题】:What is the best way to load only 1 page over https in laravel?在 laravel 中仅通过 https 加载 1 页的最佳方法是什么?
【发布时间】:2016-05-04 16:27:43
【问题描述】:

在我的 laravel 应用程序中,我需要一个页面通过 https,因为我希望用户使用他的麦克风。

getUserMedia 方法目前只允许通过 https https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins

为了性能,我只希望这个进行音频录制的页面通过 https。但这也意味着我只需要在此页面中使用“secure_asset”通过 https 加载我的所有资产。这意味着我将在我的主刀片中为我的所有资产拥有这样的东西:

@if(Request::is('record'))

   <script type="text/javascript" src="{{secure_asset('/js/jquery.js')}}"></script>

@else

   <script type="text/javascript" src="{{asset('/js/jquery.js')}}"></script>

@endif

使用 laravel 路由实现这一目标的最佳和最简洁的方法是什么?

【问题讨论】:

  • 出于性能考虑,如果你可以使用 spdy 或 http2,你应该使用 HTTPS,在这些情况下它比 http 更快。

标签: php laravel https getusermedia


【解决方案1】:

在您的 routes.php 中使用 'https' =&gt; true] 仅用于这一路线/页面 - 这对我来说似乎很干净。

例子:

Route::post('/yourroute', ['uses' => 'YourController@method', 'https' => true]);

更新 您也可以使用public 文件夹中的.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]

    # Redirect specific route to HTTPS
    # The rule is looking for the content between ^ and $ and if found in the URL it redirects to https://www.example.com/yourroute
    RewriteRule ^yourroute$ https://www.example.com/yourroute [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>

是的,对于您的资产,您需要有一个您已经使用的 if-else 语句。您可以使用secure_asset() asset(..., true) 助手。

【讨论】:

  • 并像我在问题中提到的那样加载资产?
  • 没有。资产仍然可以通过asset 加载,只需将第二个参数设置为true。请参阅function asset($path, $secure = null) - 资产方法采用第二个参数,通常为 null,但可以设置为 true 以通过 HTTPS 加载资产
  • 我这样做了,它仍然通过 http : Route::get('/record', ['as' => 'record', 'uses' => 'RecordController@index' , 'https' => true]);
  • 对不起@JimPeeters!似乎 Laravel 对此做了一些改变。可能更简单/更快的解决方案是将.htaccessRewriteRule ^yourroute$ https://www.example.com/yourroute [L,R=301] 一起使用
  • '^yourroute$' 代表什么?有没有办法像这样调用基本 url(当切换到开发模式到在线主机时?):RewriteRule https://{{BASEURL}}/record [L,R=301]
猜你喜欢
  • 1970-01-01
  • 2016-09-12
  • 2012-05-30
  • 2020-11-26
  • 2013-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-18
相关资源
最近更新 更多