我想将所有的URL请求到同一个PHP文件。 这也包括任何请求,图像和JavaScript文件,这些文件将被定向到一个PHP文件。
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.
为什么?例如,这将让我送的JavaScript文件的压缩版本的浏览器。
Why? For 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?
推荐答案路由所有的数据到一个PHP文件,以确定如何解析它给你很多更好地控制数据服务,但它也伤害了你的表现。
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.
该方法是把你的.htaccess文件如下:
The method would be to put the following in your .htaccess file:
RewriteEngine On RewriteBase / RewriteRule (.*) parse-request.php这将直接发送请求解析,request.php。更重要的是,因为该请求是没有通过一个GET变量(如解析-request.php?请求=我/ web应用/ index.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.
然后解析-request.php应该包括类似以下内容:
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... ... ... } ?>这是基本的思路。当然,你可以扩展上无限,说这样的话:如果用户登录,服务形象。如果没有,服务于注册以查看这一形象的形象。尽管正如已经说过,这将导致额外开销了很多,会减慢你的网站。更好的方法是让分析你真正想改变的任何信息,而不是单一的方法来解析一切的几种方法。例如,你的.htaccess文件:
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
通过分手了不同的脚本中工作,你只运行有什么需要,降低开销很大。此外,这种方式是不是在secretimages文件夹中的图片可以在不加载PHP解析器直接送达的Apache。同样的,你会通过,刚刚通过反正任何内容。
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.
最重要的事情,虽然是只挑选具体的项目来分析不同,你不必重新code任何网络应用程序。我向你保证,链接到MediaWiki什么的会不会像所有的URI参数由前端解析器被改变。
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.
更多推荐
我可以与htaccess的一个包罗万象的PHP脚本?
发布评论