0

I am trying to using mod_rewrite to send files to an engine.php file. I am running into an error when checking if a requested file exists with a .php extension. I will explain more after the code:

Options -Indexes +FollowSymlinks -MultiViews

<IfModule mod_rewrite.c>
RewriteEngine On

# If the requested filename is not a file
RewriteCond %{REQUEST_FILENAME} !-f
# ... but the requested filename.php is a file
RewriteCond %{REQUEST_FILENAME}.php -f
# ... rewrite it to the engine
RewriteRule ^(.*)$ engine.php?page=$1.php [QSA,L]

# If the requested script filename is a directory
RewriteCond %{SCRIPT_FILENAME} -d
# ... and an index file exists
RewriteCond %{SCRIPT_FILENAME}index.php -f
# .. rewrite to the engine
RewriteRule ^(.*)$ engine.php?page=$1index.php [QSA,L]

# If the requested script filename is a directory
RewriteCond %{SCRIPT_FILENAME} -d
# ... and an index file exists
RewriteCond %{SCRIPT_FILENAME}/index.php -f
# .. rewrite to the engine
RewriteRule ^(.*)$ engine.php?page=$1/index.php [QSA,L]

# If the file is a PHP or HTML file
RewriteCond %{SCRIPT_FILENAME} \.(php|html)$
# ... rewrite it to the engine
RewriteRule ^(.*)$ engine.php?page=$1 [QSA,L]

# If the script filename is not a file
RewriteCond %{SCRIPT_FILENAME} !-f 
# ... rewrite it to the engine
RewriteRule ^(.*)$ engine.php?page=$1 [QSA,L]
</IfModule>

I will state that I still have a way to go with mod_rewrite, so I hope that the above code isn't too bad and that my problem isn't some simple issue.
If the user requests "directory/file", but that file does not exist (e.g. the page could be stored in the database), it will work depending on a couple of variables:

  • If the file "directory.php" exists, it will send "directory/file.php" to engine.php
  • If the file "directory.php" exists, but the directory "directory" also exists, it will send "directory/file" to the engine (as wanted)
  • If neither the file "directory.php" nor the directory "directory" exist, it will send "directory/file" to the engine (as wanted)

As you can see, the error is when the file "directory.php" exists.
Hopefully this is a simple mistake I have made and that I can learn from. I guess we'll see!
EDIT
A couple of examples to hopefully make it all a little clearer. It is assumed that these all start with "http://domain.com/". These are the wanted results:
Input: "directory"
Output: "directory/index.php"
Notes: "engine.php?page=directory/index.php" exists

Input: "directory"
Output: "directory"
Notes: "engine.php?page=directory/index.php" does not exist

Input: "directory/"
Output: "directory/indes.php"
Notes: "engine.php?page=directory/index.php" exists

Input: "directory/"
Output: "directory/"
Notes: "engine.php?page=directory/index.php" does not exist

Input: "file"
Output: "file.php"
Notes: "engine.php?page=file.php" exists

Input: "file"
Output: "file"
Notes: "engine.php?page=file.php" does not exist

4

0 に答える 0