【问题标题】:Laravel installation in godaddy hosting, how to prevent accessing /public using .htaccess在 Godaddy 主机中安装 Laravel,如何防止使用 .htaccess 访问 /public
【发布时间】:2013-04-13 22:10:09
【问题描述】:

我将一个 laravel 应用程序部署到 godaddy 主机中。我使用http://forums.laravel.io/viewtopic.php?id=1258的解决方案2

现在我有一个可以使用 http://domain/http://domain/public/ 访问的 laravel 应用程序

我想关闭对http://domain/public/ 的访问,以便http://domain 提供所有服务

我尝试了这个 .htaccess 规则,但我仍然可以访问 http://domain/public/

RewriteCond %{REQUEST_URI} ^public
RewriteRule ^public/(.*)$ $1 [L]

或者,有没有办法设置 Godaddy 托管,以便我可以将公用文件夹设置为域的 documentroot?

谢谢。

【问题讨论】:

    标签: php .htaccess laravel redirect


    【解决方案1】:

    您不仅必须重写,还必须重定向客户端。这里不需要RewriteCond,因为RewriteRule 已经限制为public/ 请求而已

    RewriteRule ^public/(.*)$ $1 [R,L]
    

    但是现在你有一个无限循环,来回重写和重定向。要打破循环,您需要检测请求是否已被重写

    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule ^ - [L]
    

    把所有东西放在一起,包括来自 Laravel 论坛的规则

    RewriteEngine on
    
    # prevent endless loop
    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule ^ - [L]
    
    # redirect client to non-public
    RewriteRule ^public/(.*)$ $1 [R,L]
    
    # send real page to client
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^.*$ public/$0 [L]
    

    【讨论】:

    • 我已经尝试了上面的规则,但我仍然可以访问响应代码为 200 的 http://domain/public/ 页面。如果我访问 http://domain/public,我得到了 301 重定向到 http://domain/public/
    • @DonnyKurnia 从publicpublic/301 重定向不是来自这些规则,而是来自DirectorySlash
    • 如果你想托管多个 Laravel 实例和多个域/子域怎么办?您将如何使用 GoDaddy 托管来解决这个问题?
    • @Malchesador 我没有使用 GoDaddy 托管的经验,因此无法发表评论,抱歉。
    • @Olaf Dietsche 我在这里发布了答案:stackoverflow.com/questions/5929129/…
    【解决方案2】:

    我们决定关闭 godaddy 托管帐户并转移到 a2hosting,在那里我可以将 laravel 文件夹放在他的主目录中,在 public_html 文件夹之外。

    【讨论】:

    猜你喜欢
    • 2019-12-13
    • 2019-03-16
    • 2018-05-15
    • 1970-01-01
    • 2012-05-01
    • 2023-02-08
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多