Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Converting file path to content type

    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...)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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 Code:
    <?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....

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    in addition, since this will be solved via PHP, it should be moved to the PHP forum.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd do this with a combination of mod_rewrite and PHP.

    Firstly the mod_rewrite:
    Code:
    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:
    Code:
    //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:
    Code:
    //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.
    Last edited by brothercake; 12-20-2003 at 09:34 AM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •