如何总是重定向到https:// www。?

时间:2019-09-21 15:39:45

标签: .htaccess laravel-5

如何使我的laravel 5.8网站始终与https://www一起使用。字首 ? 在.htaccess中,我写了

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


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


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

        RedirectMatch 404 /\.git

#       RewriteRule ^$ info.php [QSA]

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

但是在某些情况下,当我运行“ mysite.com”这样的URL时,浏览器将重定向“ https://mysitede.com”,这似乎不适用于RewriteRule。 同样要确保我在我的app / Providers / AppServiceProvider.php中使用https:

class AppServiceProvider extends ServiceProvider
{
    use funcsTrait;

    public function boot()
    {

        if( $this->isHttpsProtocol() ) {  // check I am not on local server
            \URL::forceScheme('https');
        }

哪个决定?

1 个答案:

答案 0 :(得分:2)

以下.htaccess代码将请求重定向到网页的https和www版本。添加到您网站的根.htaccess文件中:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]