Redirect to a URL via htaccess if index.(html|php) doesn't exist
5,081
The -f
mode of RewriteCond
can check for file existence. You can use this to test for a file's existence. For instance, if the user requests a directory that has no index, you can redirect them with something along these lines:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index\.(html|php) !-f
RewriteRule ^ http://example.com/something-blew-up.html [R=302]
Author by
gregory
Updated on September 18, 2022Comments
-
gregory over 1 year
Is it possible to redirect (via a Rewrite Cond?) users to another URL for help if an index.html or index.php file doesn't exist within their home directory?
Thanks Greg
-
gregory over 11 yearscool, thanks! what does
RewriteCond %{REQUEST_FILENAME} -d
do? This is for any request? for instance, would the follow (assuming valid) urls get caught example.com/test/info.php or example.com/info.php -
ravi yarlagadda over 11 years@gregory The two conditions work as an "AND" pair -- it's saying, if your request is for a directory (the first condition) and there's no index file in the directory (the second condition) then send the redirect. So valid requests should never get caught, since the first rule, the directory check, won't match.
-
gregory over 11 yearsThanks! Makes sense. I wonder, if it is possible to make the browser always revalidate so the redirect isn't cached. Maybe that is for another s.o. question.