I need to create some rules in the .htaccess file so that certain conditions are met. For example, I want to show the file 404.shtml ( ErrorDocument 404 /404.shtml
) only in the case where the generated URL has the extension .html or .php (when it is misspelled of course or I don't find it on my site), in the other cases I don't ( which is when the page behaves as SPA).
Being SPA (single-page application), it always works on the same page and here I don't need to see the 404.shtml file . So I have the following and it is already working:
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
Rewritecond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
Now I want the opposite case when it is not on the page of type SPA . I would need some rule so that the 404.shtml page is seen , when the other internal pages are already and they always have the .html or .php extension. and they are not on the site (page not found).
Just as an example, it would be something like this:
IF (la pagina es SPA){
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
Rewritecond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
} else {
ErrorDocument 404 /404.shtml
}
Right now I have it like this in the .htaccess file :
...
...
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
Rewritecond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
ErrorDocument 404 /404.shtml
...but the 404.shtml is never generated when the page doesn't exist.
In short, I need that, if the URL is within those managed by the SPA, it is redirected to the SPA and in another case it is redirected to a 404.shtml page
Can something like this be done from the .htaccess file ?
Proposed solution
To solve your problem, simply exclude the real directories you have from the rule:
It can also be done with a single condition per check:
Documentation
The operation of
RewriteCond
/REQUEST_URI
:In Spanish:
Tests
We create the following files:
http://url/.htaccess
: will generate aForbidden
by the default security rules of the Apache server.http://url/historias/otra.html
: Will load the HTML file (directory rules).http://url/biografía/pruebas.html
: Will load the HTML file (directory rules).http://url/pruebas/primera.html
: Will load the HTML file (file extension rules).http://url/pruebas/segunda.php
: Will load and execute the PHP script (file extension rules).http://url/pruebas/tercera.txt
: It will go to the SPA.http://url/pruebas/cuarta.html
: Will generate a 404 error (file extension rules prevent SPA).