I have a Laravel project which was moved from custom VPS into shared hosting.
我有一个Laravel项目,这是从自定义VPS移动到共享主机。
Rewrite all from root to the public was easy part.
从根到公众重写所有内容是很容易的部分。
But when i go to the domain.com/public page works...
但当我进入Domain.com/公共页面Works...
So how i rewrite domain root to the public, but redirect direct acces to the domain.com/public back to the domain.com/ ?
那么,我如何将域根重写为公共,但将对domain.com/public的直接访问重定向回domain.com/?
Disclaimer:
There is plenty threads about Laravel and rewrite to the public folder. But no one works as expected.
免责声明:有很多关于Laravel和重写公共文件夹的帖子。但没有人能像预期的那样工作。
更多回答
Be aware of that kind of configuration: when everything is set and working as you'd think, check if https://www.example.com/.env
is accessible from client/browser. Where example.com is your arbitrary domain.
注意这种配置:当一切都设置好并按您想象的那样工作时,检查是否可以从客户端/浏览器访问https://www.example.com/.env。其中Example.com是您的任意域。
If you edit so that this is a question you can post your answer to that question. Posting what appears to be an answer as a question will confuse some people.
如果您进行编辑,以使这是一个问题,您可以发布您对该问题的答案。把看起来像是答案的东西作为问题发布,会让一些人感到困惑。
@Tpojka i tried rename index.php in public to _index.php to try the configuration. Files in root are not accessible because all is forced to the public. But i got your tip. When i access domain root i got forbidden response. When i try access something in root i got 404 (because is not in public folder..) So i think this works as expected.
@Tpojka我尝试在公共场合将index.php重命名为_index.php以尝试配置。根目录中的文件是不可访问的,因为所有文件都被强制向公众开放。但我收到了你的小费。当我访问域根目录时,我得到了禁止的响应。当我尝试访问根目录中的内容时,出现了404(因为不在公共文件夹中)。因此,我认为这符合预期。
优秀答案推荐
Because most shared hosts do not let you change the directory of the main domain, this is a common problem. With Laravel, you must point the domain to the public folder.
因为大多数共享主机不允许您更改主域的目录,所以这是一个常见问题。使用Laravel时,您必须将域指向公用文件夹。
While it is possible, be aware that with this solution you would be playing whack-a-mole with security issues were the app to ever update, as noted by @Tpojka you might be at risk of leaking secrets such as from your .env file.
虽然这是可能的,但请注意,使用这种解决方案,如果应用程序进行更新,您将在安全问题上打鼹鼠,正如@Tpojka所指出的那样,您可能会面临泄露机密的风险,例如从您的.env文件中泄露机密。
A better solution which was described in this answer is to move the Laravel app directory somewhere else and either copy or symlink the public/ folder contents. This way the app's actual files are not publicly accessible. You will need to make sure if you need to symlink storage/ that you move it to the proper document root as well.
这个答案中描述的一个更好的解决方案是将Laravel应用程序目录移动到其他地方,并复制或符号链接公共/文件夹内容。这样,应用程序的实际文件就不能公开访问了。您需要确保是否需要符号链接存储/是否也将其移动到适当的文档根目录。
I found solution so i want to share it with others.
我找到了解决方案,所以我想和其他人分享。
This have to be in the root directory.
Public directory .htaccess file have to be emtpy.
它必须位于根目录中。公共目录.htaccess文件必须为emtpy。
Like others comments here. After setup this you need check if your sensitive files like .env is not visible.
就像其他人在这里评论的那样。设置完成后,您需要检查您的敏感文件(如.env)是否不可见。
try domain.com/.env and if you dont get .env file you are good.
试试domain.com/.env,如果你没有得到.env文件,那你就好了。
<IfModule mod_rewrite.c>
# That was ONLY to protect you from 500 errors
# if your server did not have mod_rewrite enabled
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Prevent direct access to the "public" folder - redirect to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /public/
RewriteRule ^public/(.*) /$1 [R=302,L]
#RewriteRule ^public/(.*)$ /$1 [R=302,L]
# Redirect Trailing Slashes If Not A Folder...
# - but look for the file in the "public" folder
# (ensure we are not already in the "public" folder)
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{DOCUMENT_ROOT}/public/$1 !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]
# Rewrite "everything" to the "public" subdirectory if not already
# This ignores existing files/dirs in the document root
RewriteCond %{REQUEST_URI} ^/(.*)
RewriteRule !^public/ public/%1
# Handle Front Controller... (as before)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
更多回答
我是一名优秀的程序员,十分优秀!