Laravel htaccess 重写共享主机中的公共文件夹

问题描述 投票:0回答:1

我尝试找到将根目录重定向到公共文件夹的解决方案,这很简单。

最困难的部分是阻止像domain.com/public这样总是打开页面的直接访问。

我找到了一些将其放入一个文件中的提示和技巧。

此文件必须位于根文件夹中,而不是公共文件夹中,并且公共文件夹必须没有 .htaccess 文件


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

    #ak sa to pototototo tak chyba je tu
    #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>

我尝试帮助别人。

laravel .htaccess redirect mod-rewrite public
1个回答
0
投票

因为大多数共享主机不允许您更改主域的目录,所以这是一个常见问题。使用 Laravel,您必须将域指向公共文件夹。

虽然有可能,但请注意,使用此解决方案,如果应用程序要更新,您将面临安全问题,正如 @Tpojka 所指出的,您可能面临泄露秘密的风险,例如从您的 .env 中泄露秘密文件。

这个答案中描述的一个更好的解决方案是将 Laravel 应用程序目录移动到其他位置,然后复制或符号链接 public/ 文件夹内容。这样应用程序的实际文件就无法公开访问。您需要确保是否需要符号链接存储/将其移动到正确的文档根目录。

© www.soinside.com 2019 - 2024. All rights reserved.