mercredi 31 janvier 2018

Nginx Redirect all 404 to index excluding files

I just moved to Nginx from Apache and I'm quite novice. I'm trying to redirect all 404 errors to my index file, done by this:

error_page 404 =200 /index.php;

This rule also returns an HTTP 200 instead of produced HTTP 404. This works as expected excepting that all not found resources are affected by this rule too. For example: images, scripts, etc... This causes the innecessary execution of my index file. I just want to redirect 404 errors generated from innexistent URL paths. For example:

These will be handled by the rule:

http://example.com/wrongpath
http://example.com/wrong/path2

These won't be handled by the rule:

<img src='/resources/images/innexistentimage.jpg'/>
<script src='/resources/scripts/innexistentscript.js'></script>

And code may look something like this:

server{

    . . . .

    location ~* \ [NOT] .(jpe?g|png|gif|ico|js|css)${
        error_page 404 =200 /index.php;
    }

    . . . .

}

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire