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

Viewed 204

To make react router work i changed the .htacsess and added the code below :

DirectoryIndex index.html

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

then added this line

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

to make it exclude sw.js but it doesn't seem to exclude sw.js and requesting domain.com/sw.js still serves the index.html

here is my full .htaccess file :

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

%{REQUEST_URI} string starts with a leading / . You need to add a slash to the condition pattern

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