Sergei Tsibulchenko
Sergei Tsibulchenko

Reputation: 307

.htaccess send index.html instead status 404

I use angular.js and slim framework for api. My directory structure is

root/
   api/
   frontend/
   .htaccess

In root directory I have .htaccess file

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/frontend/$1 !-d [NC]
RewriteCond %{DOCUMENT_ROOT}/frontend/$1 !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /frontend/index.html [L]

RewriteCond %{DOCUMENT_ROOT}/frontend/$1 -d [NC,OR]
RewriteCond %{DOCUMENT_ROOT}/frontend/$1 -f
RewriteCond %{REQUEST_URI} !^/frontend/ [NC]
RewriteRule ^(.*)$ /frontend/$1 [L]

It work ok but when I try to get file that doesn't exists apache returns content of index.html and status 200 OK instead of 404 so code like this

<script src="/thisFileDoesntExists.js"></script>

will return content of index.html. How to retun 404 for wrong files?

Upvotes: 1

Views: 106

Answers (1)

anubhava
anubhava

Reputation: 786289

Your first rule is the culprit which is sending all non-files/directories to index.html. Remove it with a DirectoryIndex on top:

DirectoryIndex index.html
Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/frontend/$1 -d [NC,OR]
RewriteCond %{DOCUMENT_ROOT}/frontend/$1 -f [NC]
RewriteRule ^((?!frontend/).*)$ frontend/$1 [L]

Upvotes: 1

Related Questions