.htaccess redirect if file does not exist

So here is what I am trying to do. I have a simple structure and I use mod rewrite to rewrite URLs so that they point to the correct files. I have a process folder that is inside the web folder (the web folder has all the actual site files, such as images, css, etc.).

This works fine, but now I'm trying to make a default.php file to handle forms. The file works fine if I point to its form, but I want it to be able to point the form to the corresponding file, and if the file is missing, then it uses default.php

Forms are created by the class, and the action is set to the process / {ID OF FORM} .php. The idea is that anyone can create a form using the class, and if they don’t need to do anything special, the default.php file will handle the form.

I understand that I can simply set the form class to always use default.php, and then in the default.php file, I can check if the corresponding file comes out. If so, then use this file if it does not continue processing the form, but I'm still interested to find out if something like this will work on rebuilding the mod:

RewriteRule ^process/([^/]*) /web/process/$1 RewriteCond %{DOCUMENT_ROOT}/web/process/$1 !-f RewriteCond %{DOCUMENT_ROOT}/web/process/$1 !-d RewriteRule ^process/([^/]*) /web/process/default.php [L] 
+6
php .htaccess mod-rewrite
source share
2 answers

This rule should do this:

 RewriteCond %{DOCUMENT_ROOT}web/$0 !-f RewriteRule ^process/[^/]+$ web/process/default.php [L] 

If the requested path cannot be mapped to an existing file, it should be rewritten to the default file.

+14
source share

Usually you should have your RewriteCond calls before the RewriteRule. The rule is triggered if Conds are satisfied.

+2
source share

All Articles