3

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
5

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 https://serverfault.com/questions/348439/how-to-fix-apache-content-type-for-php-parsed-css.

  • Just to add, this will only work on Apache 2.2.12+. The very last line of the docs state, "Prior to 2.2.12, it was not possible to change the Content-Type header with this [Header] directive.". I was trying to get something similar to work on an old Apache 2.2.9 server and I've hit a wall!? – MrWhite Nov 3 '16 at 19:25
1

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>
  • 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 '14 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 '14 at 17:56

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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