将所有数据路由到一个PHP文件以确定如何解析它,这使您可以更好地控制如何为数据提供服务,但这也会影响您的性能。
RewriteEngine On
RewriteBase /
RewriteRule (.*) parse-request.php
这将直接向parse-request.php发送请求。更重要的是,因为请求不是作为GET变量传递的(例如
parse-request.php?request=my/webapp/index.php
parse-request.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...
...
...
}
?>
这是基本的想法。当然,你可以无限扩展,比如说“如果用户登录了,就为图像服务。如果没有,请提供“注册以查看此图像”图像。尽管如前所述,这会导致很多开销,并会减慢您的网站。更好的方法是有几种方法来解析您实际想要更改的任何信息,而不是一种单一的方法来解析所有信息。例如,对于.htaccess文件:
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
通过将工作分解到不同的脚本中,您只运行必要的内容,从而大大减少了开销。此外,这样,Apache就可以直接为不在“securimages”文件夹中的任何图像提供服务,而无需加载PHP解析器。不管怎样,任何你刚通过的内容都是一样的。
不过,最重要的是,只要选择特定的项目进行不同的解析,就不必重新编写任何web应用程序。我向你保证MediaWiki或其他什么东西
不是