【问题标题】:htaccess to Laravel public folder using https ONLYhtaccess 仅使用 https 访问 Laravel 公用文件夹
【发布时间】:2017-01-23 12:54:57
【问题描述】:

我的 Laravel 项目已经在共享托管服务器中(我知道你在想什么......)。在根文件夹中,我得到了一个 .htaccess 文件,其中包含以下代码:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteCond $1 !^(public)
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

这给了我没有“公共”的好 URL,但有一种情况: 如果我去:

www.example.com

所以它总是使用 URL www.....如果我去:

https://www.example.com

它始终使用 URL https://....

我的问题: 无论我先输入 www 还是 https,如何使 URL 始终为 https://www.example.com

【问题讨论】:

    标签: .htaccess https laravel-5.2 web-hosting public


    【解决方案1】:

    应该这样做:

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

    【讨论】:

      【解决方案2】:

      您可以通过在 .htaccess 文件中添加以下几行来做到这一点

      <IfModule mod_rewrite.c>
          RewriteEngine on
          RewriteCond %{REQUEST_URI} !^public
          RewriteRule ^.*$ https://%{HTTP_HOST}/public%{REQUEST_URI} [L,R=301]
      </IfModule> 
      

      【讨论】:

        【解决方案3】:

        将代码放在你的 laravel 根目录下的.htacces 中,它应该可以完成这项工作:

        RewriteEngine On
        
        # enforce https
        RewriteCond %{ENV:HTTPS} !on
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
        
        # use public directory as root, but don't include it in url
        RewriteCond %{REQUEST_URI} !^/public/
        RewriteRule ^(.*)$ /public/$1 [L,QSA]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-03
          • 1970-01-01
          • 1970-01-01
          • 2017-06-27
          • 2021-11-26
          • 2017-03-23
          • 2021-05-30
          • 2012-08-08
          相关资源
          最近更新 更多