...

View Full Version : Converting file path to content type



liorean
12-05-2003, 06:07 PM
I'm trying to make it so that the file path will be matched to a content type in the following pattern:

http://example.net/xml/file => http://example.net/file, text/xml;charset=utf-8
http://example.net/html/file => http://example.net/file, text/html;charset=utf-8
http://example.net/xhtml/file => http://example.net/file, use application/xhtml+xml if user agent handles it, text/html if not.

et cetera for other file formats such as css, jpeg and png. This should also work in a directory structure such that:

http://example.net/articles/xhtml/file => http://example.net/articles/file

I'm on an Apache server with php and cgi/perl, mod_rewrite enabled, but have no access to the server configuration files (only the usual .htaccess). How would I go about this? Anyone able to help me with the script handling it? (I don't know php or perl at all, not a bit...)

Celtboy
12-20-2003, 07:48 AM
Being almost 3 in the morning, I'm a wee bit too tired to fully respond. That and the fact that I'm not ENTIRELY sure what you're wanting to do...


the php pseudo-pseudo-code would be to:

define an array containing the different content types ie:


<?php
$contentTypes = array("xml","html","jpg","png","gif","xhtml");
?>

Then, using a regular expression, find which one is in the url, and then print to the header, the content-type found in the regular expression.

That'd take care of the part I understand, anyway....

Celtboy
12-20-2003, 07:48 AM
in addition, since this will be solved via PHP, it should be moved to the PHP forum.

brothercake
12-20-2003, 09:14 AM
I'd do this with a combination of mod_rewrite and PHP.

Firstly the mod_rewrite:


RewriteEngine on
RewriteRule ^article/([a-z]+)/([a-z]+)[/]? /?mime=$1&file=$2 [R,L]

So a URL like "http://www.domain.com/article/xml/whatever" will become "http://www.domain.com/?mime=xml&file=whatever"

You can modify the regex to suit your needs - and once you've got it working get rid of the " [R,L]" so that the address bar isn't updated.

Then you can set the content output like this:



//get vars
$mime = $_GET["mime"];
$file = $_GET["file"];

//mime is xhtml
if($mime == "xhtml")
{
//set private caching cos we're using data from the UA string
header("Cache-Control: private");

if (
//UA accepts application/xhtml+xml
stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")
&&
(
//and it's either Opera 7.3 or greater
eregi("opera[ \/]7\.[^012]",$_SERVER["HTTP_USER_AGENT"])
||
//or not Opera at all
!eregi("opera",$_SERVER["HTTP_USER_AGENT"])
)
)
{
//serve as xhtml+xml
header("Content-Type: application/xhtml+xml;charset=utf-8");
}

else
{
//serve as html
header("Content-Type: text/html;charset=utf-8");
}
}

//serve as xml
if($mime == "xml")
{
header("Content-Type: text/xml;charset=utf-8");
}

//serve as html
if($mime == "html")
{
header("Content-Type: text/html;charset=utf-8");
}

Then including the page data is the easy bit:


//get file var
$file = $_GET["file"];

//include file
include($_SERVER["DOCUMENT_ROOT"]."/path/to/$file.inc");

I typed most that off the top of my head, btw, but the mod_rewrite is tested ;)

I'm pretty sure, but not 100%, that you'll have to keep the actual articles in a directory other than "articles" - you shouldn't have a directory with the same name as the keyword in the URL, otherwise requests to it will get rewritten as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum