I have all my rewrite rules setup and working in .htaccess but I need to redirect all unmatched rules to a common place.
Here are the rules:
# add trailing slash
RewriteCond %{DOCUMENT_ROOT} -d
RewriteRule ^([^.]*[^/])$ $1/ [R]
# redirect directories to old site if it exists
RewriteCond %{DOCUMENT_ROOT}/old_site/$0 -d
RewriteRule ^(.*) old_site/$1 [NC,QSA,L]
#redirect files to old site if they exist
RewriteCond %{DOCUMENT_ROOT}/old_site/$0 -f
RewriteRule ^(.*) old_site/$1 [NC,QSA,L]
This works great, but trying to redirect the rest of the traffic (all unmatched rules) to index.php just over writes all the rules above:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
How can I insure this part only gets executed if nothing else (I guess except the trailing slash part) was matched?
EDIT:
As per @Jon Lin I’ve changed the last three lines to
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php
Which now works for everything but directories that don’t end in a slash. I can’t put [L] on the first rule because I need it to re-write further to a different directory.
That rule is never going to get applied because the %{REQUEST_FILENAME} is never going to be a directory and not a directory at the same time. If you want existing directories and files to not get rewritten (like if you link to an image), then include these conditions:
The rule you have is fine. But you may want to include a
[L]at the end of it.Also, the first rule you have, you are checking if the document root is a directory (it should), that means that rule will always be applied, which means
/index.phpis going to get rewritten to/index.php/. I think you may be looking for %{REQUEST_FILENAME} instead of %{DOCUMENT_ROOT}.