开发者

Can I make a catchall php script with htaccess?

开发者 https://www.devze.com 2023-01-22 22:17 出处:网络
I would like to direct all url requests to a single php file. This also includes any request to images and javascript files, which will be directed to that single php file.

I would like to direct all url requests to a single php file. This also includes any request to images and javascript files, which will be directed to that single php file.

Why? F开发者_JS百科or example, this would allow me to send a compacted version of a javascript file to browser.

How can this be done best, and what may be the the pros and cons?


Routing all data to a single PHP file to determine how to parse it gives you a LOT more control over how data is served, but it also hurts your performance.

The method would be to put the following in your .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule (.*) parse-request.php

This will send the request directly to parse-request.php. More importantly, since the request is not passed as a GET variable (such as parse-request.php?request=my/webapp/index.php) it's a little harder to trick your server and a little harder for people to realize that there is even a front-end parsing requests.

parse-request.php should then include something like the following:

<?php
    $url = parse_url($_SERVER["REQUEST_URI"]);
    $extension = substr($url["path"], strrchr($url["path"], '.')+1);
    switch($extension){
        case "zip": $ctype = "application/zip"; break;
        case "jpeg":
        case "jpg": $ctype = "image/jpg"; break;
        case "php":
        case "html": $ctype = "text/html"; break;
        case "css": $ctype = "text/css"; break;
        case "js": $ctype = "text/javascript"; break;
    }
    header("Content-type: ".$ctype);
    /*
        Now determine how to display each one
    */
    if($extension == "js"){
        // minify and output
    } else if ($extension == "php"){
        require($url["path"]); // run the PHP file
        // Note that certain variables in PHP believe you are in the subdirectory of parse-request.php
        // You have to account for this in your webapp
    } else...
       ...
       ...
    }
?>

That's the basic idea. Of course you can extend on that infinitely, saying things like "If the user is logged in, serve the image. If not, serve the 'sign up to view this image' image". Although as was already said, this causes a LOT of overhead and will slow down your website. The better method is to have several ways of parsing any information that you actually want to change, rather than a single way to parse EVERYTHING. For example, with your .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule (.*).js minify-js.php
RewriteRule (.*).css parse-css.php
RewriteRule secretimages/(.*).jpg hide-images.php
RewriteRule profile.html are-you-logged-in.php

By splitting up the work among different scripts you only run what's necessary, decreasing the overhead a lot. Also, this way any images that aren't in the "secretimages" folder can be directly served by Apache without having to load the PHP parser. Same with any content you would have just passed through, anyway.

The most important thing, though, is that by only picking specific items to parse differently you don't have to re-code any web-apps. I guarantee you that MediaWiki or something would NOT like all of their URI parameters being changed by a front-end parser.


To answer the part that's not a duplicate:

Routing all access through a single resource is called FrontController. It's a common pattern, but it's supposed to handle requests to an application and not to any resource on the webserver.

Passing all (as in really all) requests to index.php is a complete waste of resources. Your index.php will have to determine what to serve on each request, effectively making it the webserver. That's not what it is supposed to do and it is also not what it's good at.

Also, I assume you want to minify your JS file each time it is requested. Why do so when you can do that once and then serve the minified version on each request instead, making full use of the webserver's caching and compression, instead of reimplementing that from the PHP script?


htaccess RewriteRule http://forums.digitalpoint.com/showthread.php?t=25353

0

精彩评论

暂无评论...
验证码 换一张
取 消