256 Kilobytes

Answers in Frameworks | By Some Guy

Published 4 months agoSun, 02 Dec 2018 03:44:07 -0800

48 views, 0 RAMs, and 3 comments

Tags: Laravel, Redirects, 301, HTTP Status Codes

Profile Photo - Some Guy Some Guy
🗎 3223 🗨 41 🐏 31
Subscriber

For example, http://mywebsite.com should redirect to https://www.mywebsite.com

Can I do this with Laravel or do I need directly adjust server settings/files?

Download more RAM. 🐏 ⨉ 0 Posted by Some Guy 4 months ago 🕓 Posted at 02 December, 2018 03:44 AM PST

This is a patsy account used by the 256 Kilobytes staff to seed content. 

Profile Photo - Hash Brown Hash Brown
🗎 41 🗨 268 🐏 83
Staff

You can do this:

App::before( function( $request )
{
    if( ! Request::secure() )
    {
        return Redirect::secure( Request::path() );
    }
});

Add this to your filters.php file inside App::before() (if it exists).

This may not work if you run cloudflare, you may need to set a trusted proxy in this case.

Download more RAM. 🐏 ⨉ 0 Posted by Hash Brown 4 months ago 🕓 Posted at 04 December, 2018 22:48 PM PST

"THAT DOG IS GETTING RAPED" - Terry A. Davis

Profile Photo - August R. Garcia August R. Garcia LARPing as a Sysadmi... Portland, OR
🗎 104 🗨 620 🐏 116
Site Owner

The fix via .htaccess is also very straightforward. Open the included .htaccess file located at: /public/.htaccesss and then add these lines:

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

    # Redirect to www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$  http://www.%{HTTP_HOST}/$1 [R=301,L]

If you leave everything else how it is by default, the full file will look like this:

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

    RewriteEngine On

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

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

    # Redirect to www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$  http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
Download more RAM. 🐏 ⨉ 0 Posted by August R. Garcia 4 months ago 🕓 Posted at 05 December, 2018 00:35 AM PST

The CIA wants all code in the cloud under their lock and key. They want to ban compilers and make people think HTML is computer programming. - Terry A. Davis

Profile Photo - August R. Garcia August R. Garcia LARPing as a Sysadmi... Portland, OR
🗎 104 🗨 620 🐏 116
Site Owner

Update. Don't fucking use the code in the previous post I made. Use this code:

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

    RewriteEngine On
    RewriteBase /

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    #RewriteRule (https\?://)(www\.)\?(.*)/index\.php(.*)\? https://www.$3$4 [R=301,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]

    # Redirect to www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Note that it can take some amount of time for the code to actually update on your webhost. If you're not seeing changes and you are using a VPS or dedicated server, then resetting the server should force changes to take effect. Otherwise, wait briefly and it should update itself.

Download more RAM. 🐏 ⨉ 0 Posted by August R. Garcia 1 week ago 🕓 Posted at 13 April, 2019 00:01 AM PDT

The CIA wants all code in the cloud under their lock and key. They want to ban compilers and make people think HTML is computer programming. - Terry A. Davis

Post a New Comment

To leave a comment, login to your account or create an account.

Do you like having a good time?

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account
You can also login to an existing account or recover your password. All use of this site is subject to terms outlined in the terms of service and privacy policy.