开发者

ForceType/htaccess file extension question - extensionless files?

开发者 https://www.devze.com 2023-02-05 08:34 出处:网络
This is my .htaccess file: <IfModule php4/5.c> php_admin_flag Option php_flag Option php_admin_value Option

This is my .htaccess file:

<IfModule php4/5.c>
php_admin_flag Option
php_flag Option
php_admin_value Option
php_value Option
</IfModule>  
<Files .>
ForceType application/x-httpd-php
SetHandler application/x-httpd-php
</Files>

The above code somehow works, but I'm not sure why though... I expected a 500 error. I'm OK at .htaccess, but mainly for things like blocking robots/spiders etc. rather than filetypes. The top of the file is meant for custom php.ini files (I was trying to replicate on my own Apache server as if I had no access to the proper php.ini file, like they do on web hosting companies' sites, just for added realism on my testing sites).

Although I understand how to use ForceType and SetHandler, I'm not sure how to use it for extensionless files (e.g. if I had a file called testing1, I could run it as php).

Previously I did it this way:

<Files testing1&g开发者_StackOverflow中文版t;
ForceType application/x-httpd-php
SetHandler application/x-httpd-php
</Files>

but it became tedious doing it for every single extensionless file.

Basically, what I'm trying to do is to ensure that I have extensionless files via the ForceType/SetHandler directives, but is it possible? (and is the symbol above in my first example the wildcard one, or not?)

Thanks


DefaultType has been removed in Apache 2.4. Your best option is the following:

<Files *>
    ForceType application/x-httpd-php
</Files>
<Files *\.*>
    ForceType None
</Files>

This will catch all files without an extension and process them as PHP. Then all files with an extension will be processed as normal.

Using mod_mime_magic is not a good choice as each file will need to be checked each time. See the mod_mime_magic docs for more info.


For Apache 2.3 and older, just change the DefaultType as follows:

DefaultType text/html

This way, every non-recognized file (including files without an extension) will be treated as HTML.


For Apache 2.4 and up, see Tigger’s answer.


Extensionless files only

This solution affects only extensionless, statically served files: (credit Eugene Kerner)

<FilesMatch "^[^.]+$">
    ForceType application/x-httpd-php
    </FilesMatch>

Any unknown content

This one affects any response that would otherwise be transmitted without a Content-Type header. In other words, it mimics the behaviour of the old DefaultType directive:

Header set Content-Type "application/x-httpd-php" "expr=-z %{CONTENT_TYPE}"

It should be possible to use setifempty here instead of the -z expression. But it fails and overwrites the header in every response, empty or not. I don’t know why. Eric Covener says it’s because the Content-Type header isn’t added “until the very last second”.

Old servers only

This will fail after upgrading to 2.4: (see the manual)

DefaultType application/x-httpd-php


I spent ages trying to resolve a similar issue where ForceType and DefaultType wouldn't work on some pages.

I found the same answer as kbk. Edit /etc/httpd/conf/httpd.conf and comment the lines for the Mime Magic Module:

<IfModule mod_mime_magic.c>
#   MIMEMagicFile /usr/share/magic.mime
#    MIMEMagicFile conf/magic
</IfModule>

After that, restart Apache.

You can find more information here: http://realtechtalk.com/HTML_files_in_Apache_still_displaying_as_raw_text_even_with_DefaultType_ForceType_etc-1752-articles


(1) Modify .htaccess or apache2.conf or httpd.conf

Source:

  • http://helpful.knobs-dials.com/index.php/Apache_config_and_.htaccess_-_semi-sorted
  • http://hints.macworld.com/article.php?story=20040211102018600

Add: DefaultType application/octet-stream

Remove if present: DefaultType text/plain

Restart apache

(2) Enable mime_magic

Sources:

  • http://www.wellho.net/mouth/1564_Default-file-MiMe-types-for-Apache-httpd-and-Apache-Tomcat.html
  • http://httpd.apache.org/docs/1.3/mod/mod_mime.html

Enable mime_magic $ sudo a2enmod -> mime_magic

$ sudo /etc/init.d/apache2 restart

Disable a module:

$ sudo a2dismod
-> mime_magic
$ sudo /etc/init.d/apache2 restart
0

精彩评论

暂无评论...
验证码 换一张
取 消