Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond matches filename but not file extension

         

engenius

3:38 am on Jan 10, 2010 (gmt 0)

10+ Year Member



The problem I am having is that the url (http://www.example.com/counseling/admissions-guide) should be directed to index.php but the url (http://www.example.com/counseling/admissions-guide.pdf) should go to the file, but both urls go straight to the pdf.

I am using the rewrite rules from the Zend Reference Guide:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

I thought the first RewriteCond was supposed to check if a file exists but it seems to be ignoring the file extension.

Has anyone else experienced this problem or have any ideas how to fix it? Please don't say rename the file or directory. I really can't do that. I also need this to work for all file extensions, not just pdf.

Thanks.

jdMorgan

4:38 pm on Jan 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Disable MultiViews for a start...

Options -MultiViews

If you're hosted on Apache 2.0 or above, you may also need

AcceptPathInfo off

Note that you should make a list of all existing files that will always exist on your site and which will never need to be processed by your script, and exclude them from the rules above. Such files as robots.txt, sitemap.xml, and perhaps images, css files, and media files can be excluded.

This prevents the *very* wasteful invocation of *six* calls to your operating system to go check the disk for each and every page, object, or file requested from your server. You may see a very-noticeable speed-up from this simple change:


RewriteEngine on
#
# Skip rewrite to /index.php if request is for /index.php itself or if requested
# URL resolves to an existing non-zero-size file, symbolic link, or directory.
[i]RewriteCond $1 ^index\.php$ [OR][/i]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [S=1]
# Else rewrite all requests to /index.php
RewriteRule ^.*$ index.php [L]

Adding the new RewriteCond at the top prevent the OS -exists calls from being made after this code has already executed once, has rewritten the request to /index.php, and is re-invoked (as mod_rewrite code in .htaccess always is after any rule matches and is applied). This one line essentially cuts the number of -exists checks in half.

See explanation and discussion of almost-identical problem with WP and Joomla code here [webmasterworld.com].

Jim

engenius

4:11 pm on Jan 11, 2010 (gmt 0)

10+ Year Member



I made both changes and everything is working perfectly. Thanks so much.