title: .htaccess for an SPA, rewrite incoming requests to index.html, except for static resources
body: <p><strong>EDIT 1</strong> Thank you very much for your responsiveness and patience!.</p>
  
  <p>I am developing a SPA router, so everything happens in index.html. It uses HTML5 History API to rewrite the history and it would be awesome to create SEO friendly URLs. </p>
  
  <p>Some sections are already on the index.html and others are loaded dynamically with ajax or fetch (if that matters)</p>
  
  <p>The app has the next basic directory structure</p>
  
  <pre><code>assets/ 
  css/
  img/
  js/
  modules/
  partials/
  vendor/
  index.html
  </code></pre>
  
  <p>Now in the router mainly there are sections and subsections, I am rewriting sections as a subdirectory, and subsections as sub-subdirectory</p>
  
  <pre><code>example.com/index.html &lt;--- where the magic happens
  </code></pre>
  
  <p>But index.html is also ugly, it would be preferable to rewrite it to (not really needed as it happens naturally, but nice to have) </p>
  
  <pre><code>example.com R=301 &lt;permanent
  </code></pre>
  
  <p>Then, when the user navigates to a section the url will be rewritten by history api as:</p>
  
  <pre><code>example.com/customers
  </code></pre>
  
  <p>But if the user reloads the website, he gets error 404</p>
  
  <p>Then, when the user navigates to a subsection of that section he will see in the url bar</p>
  
  <pre><code>example.com/customers/casestudy
  </code></pre>
  
  <p>But again, if the user reloads, he gets error 404</p>
  
  <p>What I want is to permanently re-write those URLs to index.html and its router/state manager will restore(load/show) the correct state.</p>
  
  <p>The goal of this, is that URLs are SEO friendly and that search engines can index correctly each section of the website. Also if we need to hand a URL of our site to a customer/partner/stakeholder/anybody</p>
  
  <pre><code>example.com/mydata is better than example.com/index.html/#/dataFragment25654789
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <p><strong><em>EDIT 02</em></strong>
  The next .htaccess works fine</p>
  
  <pre><code>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
  </code></pre>
  
  <p>except when trying to load a subsection</p>
  
  <p>example.com/customer/mydata</p>
  
  <p>Then it gets uncaught SyntaxError: Unexpected Token '&lt;' the same as @MrWhite answer</p>
  
  <p><strong><em>OLD QUESTION</em></strong></p>
  
  <p><em>I want to permanently <strong>rewrite</strong> requests to any file or subdirectory to my <code>index.html</code> as it will dispatch the correct views, but preserving the subdirectory structure, no files allowed. I can't get it to work especially with sub-subdirectories
   - <code>example.com/index.html</code> or <code>anyother.file</code> ---> <code>example.com/</code>
   - <code>example.com/mydirectory/</code> or <code>example.com/mydirectory/anyfile.file</code> to <code>/index.html</code> URL should look <code>example.com/mydirectory</code>
   - <code>example.com/mydirectory/mydirectory</code> or <code>example.com/mydirectory/anyfile.file</code> to <code>/index.html</code> URL should look <code>example.com/mydirectory/mydirectory</code></em></p>