skipping mod rewrite entirely?

New to mod_rewrite? This is a good place to start.

skipping mod rewrite entirely?

Postby jon23d » Sun Sep 27, 2009 2:48 pm

I've spent days writing an .htaccess file, the only real reason I'm using more than one rule is to allow apache to handle mime-type, content negotation, etc., for resources that aren't parsed php files.

I'm wondering if anyone has had any experience with the php virtual() function. I was able to request in image from a php file without worrying about mime-type like this:

Code: Select all
$file = "search.png";
   $file_info = apache_lookup_uri($file);
   header('content-type: ' . $file_info -> content_type);
   virtual($file);


Of course, when I redirect all requests, this subrequest is being redirected as well. I'm sure there's a reasonably easy way around this, but is this method problematic in any way? Does it cause too much overhead? This would allow me to quickly skip all sorts of weirdness if it is practical.

Thanks

Edit:

The NS flag took care of subrequests, so it seems that this is all that is needed:

Code: Select all
RewriteRule ^(.*)$ index.php [QSA,L,NC,NS]


Edit 2:

Well hell, once I've got the mime-type I realize I can just call readfile(), so now we're only making one request from apache with apache_lookup_uri(), surely this has got to be okay!?!?
jon23d
 
Posts: 19
Joined: Sun Feb 01, 2009 11:36 pm

Postby richardk » Mon Sep 28, 2009 2:09 pm

I think readfile() will be ok as long as you use $file_info->filename in case there has been content negotiation and it is different.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jon23d » Mon Sep 28, 2009 2:17 pm

Yeah, I think this will make for a much easier time in the future, here's the final function I came up with:

Code: Select all
      /**
       * Serve the provided file to the browser
       *
       * Headers cannot have been sent
       *
       * If $force_save_dialog is true, then the browser
       * will show a save dialog instead of rendering the contents
       *
       * @static
       *
       * @throws FrameworkException
       *
       * @param STRING $filename_with_path
       * @param BOOL $force_save_dialog
       * @return VOID
       */
      static function serveFile($filename_with_path, $force_save_dialog = false) {
         // does the file exist?
         if (!is_file($filename_with_path)) $message = "Invalid filename";
         // have headers already been sent?
         if (headers_sent()) $message = "Headers already sent";
         // uh-oh
         if (isset($message)) throw new FrameworkException($message);
         // get mime-type, test for readibility
         $file_info = apache_lookup_uri($filename_with_path);
         if (!$file_info->filename) throw new FrameworkException("Unable to read file");
         // everything looks okay, go ahead and send the file
         $filename = $file_info->filename;
         // send the mime-type
         header('Content-type: ' . $file_info -> content_type);
         // force save if requested
         if ($force_save_dialog) header("Content-Disposition: attachment; filename=\"$filename\"");
         // output file
         readfile($filename_with_path);
      }


And the new htaccess:

Code: Select all
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On

# PHP settings
php_value session.use_cookies 1
php_value session.use_only_cookies 1
php_value session.cookie_httponly 1
php_value session.save_path "./sessions/"

RewriteRule ^(.*)$ dispatcher.php [QSA,L,NC]


This makes me much happier :)
jon23d
 
Posts: 19
Joined: Sun Feb 01, 2009 11:36 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 25 guests

cron