skip to Main Content

I have a .htaccess file that redirects every requests to ./core/index.php. But for static files which are in ./node_modules/ and ./assets/ folders, I want to enable normal redirect for them.

So,

  1. if request: url/whatever/page/ -> redirect to: core/index.php
  2. if request: url/assets/image/file.png -> get: assets/image/file.png
  3. if request: url/node_modules/css/file.css -> get: node_modules/css/file.css

My current code:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ core/index.php [L,QSA]

2

Answers


  1. So what you can do, is first check if it goes to assets, then redirect there, then for node_modules. And if now of that, then you redirect to the index if none of the conditions met:

    RewriteEngine On
    
    RewriteCond %{DOCUMENT_ROOT}/assets/$1 -f
    RewriteRule ^(.+) assets/$1 [L]
    
    RewriteCond %{DOCUMENT_ROOT}/node_modules/$1 -f
    RewriteRule ^(.+) node_modules/$1 [L]
    
    RewriteCond %{THE_REQUEST} s/assets/ [NC,OR]
    RewriteCond %{THE_REQUEST} s/node_modules/ [NC,OR]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ core/index.php [L,QSA]
    
    Login or Signup to reply.
  2. I am not really sure what you mean by "I want to enable normal redirect for them" … What you actually show as examples looks to me as if you do not want any rewriting or redirection to get applied at all for those two base paths. If so, then there are two alternatives for this:

    Either use conditions:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/assets/
    RewriteCond %{REQUEST_URI} !^/node_modules/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ core/index.php [L,QSA]
    

    Or explicit exceptions:

    RewriteEngine On
    RewriteRule ^/?assets/ - [END]
    RewriteRule ^/?node_modules/ - [END]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ core/index.php [L,QSA]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search