小编典典

Laravel 托管在子目录中

php

客户请求建立一个网站,现在它已经建立,他们要求将其与现有网站一起托管,但在子目录 ( example.com/example/path) 中。

我现在正在处理的是找出正确托管它所需的 apache 规则。我已将整个项目结构上传到该目录。在公共目录 ( /example/path/public) 中,我有以下 .htaccess 文件(此后我将其称为 Laravel .htaccess):

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On
    RewriteBase /example/path

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


    # Serve Cached Page If Available...
    RewriteCond %{REQUEST_URI} ^/?$
    RewriteCond %{DOCUMENT_ROOT}/page-cache/pc__index__pc.html -f
    RewriteRule .? page-cache/pc__index__pc.html [L]
    RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f
    RewriteRule . page-cache%{REQUEST_URI}.html [L]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

<FilesMatch ".(jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf|eot|otf)$">
    Header set Cache-Control "max-age=2628000, public"
</FilesMatch>
<FilesMatch ".(css|js)$">
    Header set Cache-Control "max-age=86400, public"
</FilesMatch>

我已经添加了RewriteBase基于我试图找到解决方案的行。

在基本 .htaccess 文件 ( /example/path/.htaccess) 中,我有以下内容:

<IfModule mod_alias.c>
    Alias /example/path /home/www/<user>/html/example/path
    <Directory "/home/www/<user>/html/example/path">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</IfModule>

然而,问题是这会返回一个内部服务器错误。我尝试了其他不使用它的潜在解决方案,Alias而是返回 40X 错误。

当用户访问时,我需要能够让 Laravel 站点完全正常工作example.com/example/path。在过去的 90 分钟内,我尝试了许多解决方案,但都没有奏效。

我的情况的正确解决方案是什么?


阅读 168

收藏
2022-07-25

共1个答案

小编典典

在公共文件夹中,我们托管了 8 个平台,每个平台的根目录都有这个.htaccess配置。希望这可以帮助你。

<ifmodule mod_rewrite.c>

    <ifmodule mod_negotiation.c>
        Options -MultiViews
    </ifmodule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</ifmodule>
2022-07-25