【问题标题】:Redirect website http to https in laravel in shared hosting using htaccess使用 htaccess 在共享主机中将网站 http 重定向到 laravel 中的 https
【发布时间】:2019-08-23 15:12:18
【问题描述】:

我有一个 Laravel 项目。我用了Godaddy。最近,我在我的网站上安装了一个 SSL 证书,所以当我在浏览器中输入 https://example.com 时它可以工作,但是当我编写 example.com 时,它通过 HTTP 连接。

为了解决这个问题,我将这些行添加到我的根文件夹的 .htaccess 文件中:

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

但这会重定向到我https://example.com/public

我添加了两个 htaccess 一个在根文件夹中,第二个在公共文件夹中。

第一个 htaccess 代码:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L,NC]

第二个htaccess代码是:

选项 -MultiViews -Indexes

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} (.+)/$
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^ %1 [L,R=301]


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

我怎样才能做到这一点https://example.com

提前致谢!

【问题讨论】:

标签: php laravel apache ssl


【解决方案1】:
<IfModule mod_rewrite.c>
    RewriteEngine on
    Options +FollowSymlinks
    #redirect http non-www to https://www
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?your-domain\.com$
    RewriteRule (.*) https://www.your-domain.com/$1 [R=301,L]
    #redirect https non-www to www
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^your-domain\.com$
    RewriteRule (.*) https://www.your-domain.com/$1 [R=301,L]
</IfModule>

我正在使用它来重定向。

【讨论】:

    【解决方案2】:

    试试这个代码:

    RewriteCond %{SERVER_PORT} ^80$
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^/(.*)$ http://%1/$1 [R]
    RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
    

    【讨论】:

      【解决方案3】:

      这是解决方案,

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

      【讨论】:

        【解决方案4】:

        尝试了所有解决方案,但这在 Laravel 6.0 上对我有用,通过在根文件夹中创建 .htaccess 文件而不是 /public/.htaccess 文件来使用它。 p>

        Options +SymLinksIfOwnerMatch
        RewriteEngine On
        
        # ensure www.
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
          
        # ensure https
        RewriteCond %{HTTP:X-Forwarded-Proto} !https
        RewriteCond %{HTTPS} off
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
        RewriteRule ^ index.php [L]
        

        【讨论】:

          猜你喜欢
          • 2021-03-19
          • 1970-01-01
          • 1970-01-01
          • 2018-09-20
          • 1970-01-01
          • 2019-03-18
          • 2012-10-11
          • 2015-11-10
          • 2018-08-31
          相关资源
          最近更新 更多