使用 htaccess 从文档根目录重定向到文件夹

Redirecting from document root to folder with htaccess

我的目录结构是这样的,我的文档根目录是/

/
.htaccess
    /public

这一切都在我的本地 xampp 服务器上。 服务器设置为侦听 mydomain.local,在我的主机文件中,我已将 mydomian.local 指向本地主机。

我正在使用以下 .htaccess 代码,当使用 mydomain.local 作为地址时它工作正常。

我遇到的问题是当我使用 localhost/mydomain 作为地址时它不起作用

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php?path= [NC,L,QSA]

当使用 localhost/mydomain 作为地址时,我怎样才能让它工作?

这是因为我正在尝试注册 Service Worker,但它们只能通过 https 或本地主机工作。

提前致谢

我修好了。

根文件夹.htaccess

RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule ^(.*) public/ [L]

public 文件夹.htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path= [NC,L,QSA]

谢谢

您也可以通过稍微更改您的重写规则以在匹配中添加一个可选的 mydomain/ 但不使用它来将单个 .htaccess 文件保留在根目录中:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Add optional group
RewriteRule ^(mydomain/)?(.*)$ /public/index.php?path= [NC,L,QSA]
# Same with a non capturing group
RewriteRule ^(?:mydomain/)?(.*)$ /public/index.php?path= [NC,L,QSA]

顺便说一下,您可以使用 htaccess tester 检查规则重写对 url

的影响