A fairly basic htaccess question, but I don't use it much, so i have no idea. I want to check if the requested file exists. If it does, forward to one page, and if not forward to another page and pass the requested path as GET.
Thanks in advance.
A fairly basic htaccess question, but I don't use it much, so i have no idea. I want to check if the requested file exists. If it does, forward to one page, and if not forward to another page and pass the requested path as GET.
Thanks in advance.
RewriteEngine On
# check if requested file or directory exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# if not, pass it to index.php
RewriteRule ^(.*) index.php?page=$1 [QSA]
As Gumbo suggested, you can repeat the condition without the !
if you also want to rewrite the URL when a file exists. Maybe you want to 'protect' your real files and folders with this method.
Use the -f
expression in a RewriteCond
condition to test if the given path points to an existing regular file:
RewriteEngin on
# file exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ one-page [L]
# file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ another-page [L]
The initial requested URI should then be available in an environment variable.