I'm trying to redirect all requests to a script if the requested file or directory doesn't exist, but I need to improve this .htaccess in order to check if the file exists on the parent folder.
I currently have:
RewriteEngine on
# check if requested file or directory exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# map requests to contentpage.php and appends it as a query string
RewriteRule ^(.*)/(.*)$ contentpage.php?url=$2&lang=$1
now, this .htaccess successfully redirects
en/products.php -> contentpage.php?url=products.php&lang=en only if products.php doesn't exists on the child directory /en/ (and actually the directory /en/ doesn't physically exists, so the RewriteCond fails to filter when I have a file products.php on the parent directory).
I need to check if the file exists on the top directory where my files are. It needs to redirect only the language directories, like /en /es /de /pt .etc
Anyone has an idea how to solve this? thanks
You can put your rules in a htaccess file in your top directory; this way, if you have
/parentdir
/en
/es
/de
even if you access the parent path or the child paths:
the /parentdir/.htaccess rules are evaluated
So you can use a ruleset like this:
# check if requested file does not exists in the child dir
# check if requested file does not exists in the child dir
# if does not exists... check if it also does not exists in the parent dir
RewriteCond /parentdir/$1/$2 !-f
RewriteCond /parentdir/$2 !-f
RewriteRule ^/parentdir/(es|en|fr|de)/(.*)$ contentpage.php?url=$2&lang=$1
# check if requested file does not exists in the parent dir
RewriteCond /parentdir/$1 !-f
RewriteRule ^/parentdir/(.*)$ contentpage.php?url=$2&lang=$1