Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I currently use the following Apache httpd conf:

<FilesMatch "\.svg$">
  SetHandler application/x-httpd-php
</FilesMatch>
AddType image/svg+xml .svg

What I'm trying to achieve:

  • Parsing the PHP scripts in my SVG files
  • Serving my SVG files with the image/svg+xml MIME type

However, the AddType seems to be ignored now that I've set it to use the alternative handler for my SVG files. They're now served as text/html.

How can I make the httpd parse the PHP in my SVG files while preserving its image/svg+xml type?

I currently include a header("Content-type: image/svg+xml"); call in my SVG file to solve this, although I'd rather have a global way of doing this instead of having to insert this into all of my SVG files.

  • CentOS 6.5
  • Apache 2.2.15
  • PHP 5.3.3
share|improve this question

2 Answers 2

up vote 3 down vote accepted

I ended up setting the handler for SVG files to application/x-httpd-php, and used apache's mod_headers to pass the correct content-type header.

<Files "*.svg">
  SetHandler application/x-httpd-php
  Header set Content-Type "image/svg+xml"
</Files>

This was solved when I found a nearly identical question at http://serverfault.com/questions/348439/how-to-fix-apache-content-type-for-php-parsed-css.

share|improve this answer

The Apache mod_mime documentation states that SetHandler overrides AddType:

While mod_mime associates metadata with filename extensions, the core server provides directives that are used to associate all the files in a given container (e.g., <Location>, <Directory>, or <Files>) with particular metadata. These directives include ForceType, SetHandler, SetInputFilter, and SetOutputFilter. The core directives override any filename extension mappings defined in mod_mime.

You should be able to use the header directive from mod_headers in .htaccess to make it work:

<FilesMatch "\.svg$">
    SetHandler application/x-httpd-php
    Header set Content-Type "image/svg+xml"
</FilesMatch>
share|improve this answer
    
Thank you for your answer! I tried to do as you suggested, but it kept serving it as text/html. Could this be because the PHP handler will overwrite this value? –  Patrickdev May 13 at 17:08
    
I'm glad you found the solution. I hadn't tested the ForceType myself. It looks like your Header set Content-Type "image/svg+xml" is the correct solution. I'll edit that into my answer as well. –  Stephen Ostermiller May 13 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.