使用 .htaccess 内部重定向更改文档根目录无法正常工作

change document root with .htaccess internal redirect not working properly

各位开发者大家好, 我需要更改我的文档根目录中的某个特定文件夹,该文件夹在共享主机上保存我的 laravel 项目的源代码,因为我的托管计划不允许我更改默认文档根目录,所以我无法更改我的默认索引,所以我想必须使用 .htaccess

重定向它

到目前为止,我已经能够使用以下代码进行内部重定向

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ master/project/public/index.php

这样做我可以重定向到所需的文件夹,但由于某些原因它不适用于资产,我一直在 404 所有资产

master/project/public/css/[all styles]
master/project/public/js/[all scritps]
master/project/public/images/[all pictures]
master/project/public/fonts/[all fonts]

如何在不更改网址的情况下实现此目的,

内部重定向有效,但我无法弄清楚为什么我的所有资产都返回 404

试试这个 我正在使用共享主机,我正在使用它来将资产重定向到 public 目录

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

    #all request to public dir
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/ [L]

    RewriteEngine On

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

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

</IfModule>

将所有请求重定向到 public

 #all request to public dir
 RewriteEngine on
 RewriteCond %{REQUEST_URI} !^public
 RewriteRule ^(.*)$ public/ [L]

所以我找到了解决问题的方法, 这个解决方案是我最终得到的。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !master/project/public/
RewriteRule (.*) /master/project/public/ [L]

通过这种方式,您可以使用 .htaccess 文件更改主机的默认根目录。希望对 .htaccess

的新手有所帮助