Search code examples
phplaravelhttpsgetusermedia

What is the best way to load only 1 page over https in laravel?


In my laravel application I need one page to be over https because I want the user to use his microphone.

The method getUserMedia and is currently only allowed over https https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins

For performance I only want this page that does audio recording to be over https. But that also means that I need to load all my assets over https with 'secure_asset' in this page only. That means I will have something like this in my master blade for all my assets:

@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

What is the best and cleanest way to achieve this with laravel routing?


Solution

  • Use 'https' => true] in your routes.php just for this one route/page - that seems very clean to me.

    Example:

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

    Update You can also use the .htaccess file in your public folder.

    <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>
    

    And yes, for your assets you need to have an if-else statement as you already using. You can either use secure_asset() asset(..., true) helper.