In some pages I have seen (like those of GitHub and Wikipedia), there are some links that end in .js
, .jpg
etc. which means clicking them should show me the JS file or the image (and so on). But, as we all know, they display a page (the needed resources are on the page, anyway)开发者_如何学运维.
Can anyone please explain how this is done? Does it have anything to do with renaming a link?
If it's server-side coding (which I think it is), I use PHP. Thanks in advance.
It'll use URL re-writing with mod_rewrite. See http://httpd.apache.org/docs/current/mod/mod_rewrite.html
In order for those links to be mapped to your PHP scripts, you need mod_rewrite (Assuming you are on Apache)
The http header called Content-Type
tells the browser how to interpret the data. If it is set to text/html
, it will render as a webpage.
In PHP, you can use the header() function:
header("Content-Type: text/html");
Similarly, you can have a PHP script output an image:
header("Content-Type: image/jpeg");
This type definition is called MIME Type.
精彩评论