XML PHP issues solved! Oct07 '05
I finally figured out my XML/PHP problems, which have been plaguing me for years. It’s not that the solution was that difficult, it’s just that when I did find time to work on it, I could never seem to get anywhere.
I hope I don’t find any kinks, in the near future - otherwise my excitement level will be "shot down." As far as I can tell, things are working smoothly.
My main RSS feed (for full posts on this site) is currently being rendered as XML, but functioning as PHP. This means the browser will treat the file as XML (and allow for styling via XSL), but also allow the use of PHP - which lets me dynamically pull entries from the database.
This is a huge development, for me. I can’t tell you how excited I am to start using XSL more frequently.
The solution
The answer lies with htaccess. I have an .htaccess file at the root of my domain, which re-directs all requests to a single index file. This is "virtual folders," in action.
Now, even though I want the majority of requests to re-direct to the main index file - there are still some requests that don’t need a re-direct. For example: image files, CSS files, MP3 files, etc. When you request any of those files in a browser, you’d like to be taken directly to that file.
Or, in the case of a CSS file - the browser needs to request that file, in order to utilize the style sheets.
Well, XML is the same way. When a person (or aggregator) requests an XML file at my site - it needs to see the exact file.
mod_rewrite
So, in order to allow some files to show through, and others not, we can use a simple mod_rewrite rule:
RewriteRule !.(xml|css|jpg|mp3)$ /home/matthom.com/index.php
The rule above re-directs all requests, except for the ones in parenthesis. You can add as many extensions as you need inside the parenthesis, and mod_rewrite will respectfully follow your commands.
So, in my case, I needed xml inside those parenthesis.
Treat this as PHP!
Also, in that same .htaccess file, I included .xml under the list of extensions that should be treated as PHP:
AddType php-cgi .html .htm .xml
The line above treats any file with those extension as PHP - meaning you can use dynamic capabilities, inside a presumably static file type.
Sneaky, eh?
XML Stylesheet Language
Then, in order to allow XSL files to style our XML files - we need to also include xsl inside the parenthesis, because they will be requested by the browser, as well:
RewriteRule !.(xml|xsl|css|jpg|mp3)$ /home/matthom.com/index.php
XML declaration
Finally, inside our XML files, we need to make sure to specify a header of Content-type: text/xml, as well as escape the XML declaration:
<?php
header('Content-type: text/xml');
print ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
print ("<?xml-stylesheet type=\"text/xsl\" href=\"matthom.xsl\"?>\n");
?>
Notice how we use PHP, to echo the XML declaration. This is extremely important. The question mark (?) used to denote PHP code is the same as denoting XML code:
<?php
<?xml version=...
If these two were declared as is, it would cause a giant rendering bump.
Categories: Apache
, PHP
, Web Development
, XML ![]()
Add Feedback (view all)
Leave feedback
matthom
is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago.
Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.
Similar Entries
- PHP: Skipping index page call in URL (148 recent visits)
- PHP project: convert times to numbers (302 recent visits)
- PHP – passing variables across pages (8615 recent visits)
- Install Apache, PHP, MySQL on Windows (167 recent visits)
- Code mnemonics: PHP implode/explode (378 recent visits)
- Swap banner image with CSS and PHP (658 recent visits)
Stats
5 unique visits since August 2008