laravel 在godaddy托管中安装Laravel,如何防止使用.htaccess访问/public

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16122863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:43:26  来源:igfitidea点击:

Laravel installation in godaddy hosting, how to prevent accessing /public using .htaccess

php.htaccesslaravelredirect

提问by Donny Kurnia

I deploy a laravel application into godaddy hosting. I use Solution 2 from http://forums.laravel.io/viewtopic.php?id=1258

我将一个 Laravel 应用程序部署到 Godaddy 托管中。我使用来自http://forums.laravel.io/viewtopic.php?id=1258 的解决方案 2

Now I have a working laravel application that accessible either using http://domain/and http://domain/public/

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

I want to close the access to http://domain/public/so all will be served by http://domain

我想关闭对http://domain/public/所有访问的服务http://domain

I try this .htaccess rule but I can still access http://domain/public/

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

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

Or, is there any way to set up godaddy hosting so I could set the public folder as documentroot for the domain?

或者,有什么方法可以设置godaddy托管,以便我可以将公共文件夹设置为域的documentroot?

Thanks.

谢谢。

采纳答案by Donny Kurnia

We decide to close godaddy hosting account and move to a2hosting where I could put the laravel folders in he home directory, outside of public_html folder.

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

回答by Olaf Dietsche

You must not only rewrite, but also redirect the client. The RewriteCondis not needed here, because the RewriteRulealready restricts to public/requests only

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

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

But now you have an endless loop, rewriting and redirecting back and forth. To break the loop, you need to detect, if the request was already rewritten

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

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

To put everything together, including the rule from the Laravel forum

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

RewriteEngine on

# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# redirect client to non-public
RewriteRule ^public/(.*)$  [R,L]

# send real page to client
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^.*$ public/##代码## [L]