【问题标题】:Laravel remove public from url and force https in .htaccessLaravel 从 url 中删除 public 并在 .htaccess 中强制 https
【发布时间】:2020-01-20 07:51:17
【问题描述】:

如何从 url 中删除 /public/ 并自动强制 http 到 https?

我的根 .htaccess(我创建了它)

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

public/.htaccess (默认 laravel)

<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]
</IfModule>

但它不起作用 - 没有自动 http 到 https 重定向。

所以我需要结合从 url 中删除公用文件夹和使用 .htaccess 强制 http 到 https

【问题讨论】:

标签: php laravel .htaccess laravel-6


【解决方案1】:

您遇到的问题是您已将文档根目录设置为项目的根目录。在您的 apache 虚拟主机配置中,您需要将文档根目录设置为公共目录。

例如:

DocumentRoot /var/www/yourproject/public

这样,公共就不会出现在 URL 中。

如果您使用共享主机,您需要将项目的根目录移至public_html/ 上方,删除public_html,然后将public 目录重命名为public_html。然后,您可以修改引导文件以使用此路径。您也可以对其进行符号链接。

【讨论】:

  • 我的主机不允许更改 apache/nginx 设置。而且我无法删除任何默认文件夹。我只能使用 htaccess
  • 不能删除public_html
【解决方案2】:

将这两行添加到您的公共文件夹htaccess 文件中

 RewriteCond %{HTTPS} !=on
 RewriteRule .* https://yourdomain.com%{REQUEST_URI} [R,L]

你的public/.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}]

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

    RewriteCond %{HTTPS} !=on
    RewriteRule .* https://yourdomain.com%{REQUEST_URI} [R,L]
</IfModule>

【讨论】:

    【解决方案3】:

    我认为您需要使用

    启用 rewrite_url
    $ sudo a2enmod rewrite
    

    重启你的apache服务器

    $ sudo systemctl restart apache2
    

    您的站点启用/000-default.conf

     DocumentRoot /var/www/html/yourapp/public
    

    也许你可以使用

    RewriteBase "yourapp/public/"
    

    或参考this答案。

    Rename server.php in your Laravel root folder to index.php
    Copy the .htaccess file from /public directory to your Laravel root folder.
    

    【讨论】:

    • 在安装中重写
    • @Teretto 文档根目录也设置了?或者你可能需要在 htaccess 中使用 rewrite base。
    • 我无法在主机中修改 000-default.conf
    • @Teretto 你可以在你的 htaccess 中设置 RewriteBase,这可能会完成这项工作。
    • @Teretto 使用这个会影响 SSL 配置?
    【解决方案4】:

    将此代码放在域根目录的.htaccess文件中,用于删除'public:

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

    为了从 http 重定向到 https,请在位于 laravel 项目公共目录的 .htaccess 文件的顶部添加以下行。

    RewriteCond %{HTTP_HOST} your_domain\.com [NC]
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.your_domain.com/$1 [R,L]
    

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 2020-01-17
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 2020-10-10
      • 2015-01-14
      • 2020-06-07
      相关资源
      最近更新 更多