如何更改我的 .htaccess 以从 root 访问 serviceworker 文件

How to change my .htaccess to access serviceworker file from root

为了使 React 路由器正常工作,我更改了 .htacsess 并添加了以下代码:

DirectoryIndex index.html

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [L,QSA]

然后添加了这一行

    RewriteCond %{REQUEST_URI} !^sw\.js$ 

使其排除 sw.js 但它似乎并没有排除 sw.js 和请求域。com/sw.js 仍然服务于 index.html

这是我的完整 .htaccess 文件:

DirectoryIndex index.html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^sw\.js$ 
RewriteRule ^(.*)$ /index.html [L,QSA]

%{REQUEST_URI} 字符串以 / 开头。您需要在条件模式中添加斜线

RewriteCond %{REQUEST_URI} !^/sw\.js$