7

While browsing directories in Apache I would like to have the files that have no extension to be treated by the server as php when clicking on them (those files containing php code).

jck
  • 541
  • 6
  • 21
  • 2
    This is a very bad idea. What would you want to do that? Why not, as it is usually done, call them what they are, php files, and use URL rewriting to get clean URLs? – arkascha Nov 26 '15 at 08:16
  • @arkascha It is for my website, I would like to add some readme files, but I want them to appear without the .php extension as it is open for public: [http://jck.smalk.co/projects/e-learning/](http://jck.smalk.co/projects/e-learning/) – jck Nov 26 '15 at 08:46
  • @arkascha I think that I will follow your advice to not doing that, and use jQuery instead to hide the filename extension in the list, as I already use jQuery to beautify my Apache directory – jck Nov 26 '15 at 09:08
  • _Why?_ Why not use URL rewriting, as already mentioned? You shouldn't hand out physical directory indexes anyway, I'd say... – arkascha Nov 26 '15 at 09:30

2 Answers2

4

You can use the H flag of apach mode rewrite to force all files without an extension to be parsed by The php handler :

try the following in htaccess :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\. - [H=application/x-httpd-php]

on some servers, you have to change httpd-php by httpd-php5 :

RewriteRule !\. - [H=application/x-httpd-php5]
jck
  • 541
  • 6
  • 21
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • It doesn't work, the file still opens as html: [http://jck.smalk.co/projects/e-learning/](http://jck.smalk.co/projects/e-learning/) – jck Nov 26 '15 at 08:49
  • Is that an html file? with .html extension or without any extension? – Amit Verma Nov 26 '15 at 08:53
  • it is a php file without extension – jck Nov 26 '15 at 08:54
  • @jck the code is working fine on my apache, i tested it with extension less php script files. Where are you putting this? make sure this is the first rule in your htacc – Amit Verma Nov 26 '15 at 09:12
  • yes I putted your code at beginning of the htaccess file in root directory, and the extension less php file still show like that: [http://jck.smalk.co/projects/e-learning/test](http://jck.smalk.co/projects/e-learning/test) If you check source file, you can see that the php snippet get commented – jck Nov 26 '15 at 09:22
  • yes its not being parsed, Does it work when you add .php to it ,test.php? or may be php is not enabled|Htaccess is not supported. – Amit Verma Nov 26 '15 at 09:30
  • yes it works good when I add .php. Btw I tested your snippet to see if the RewriteRule and Regex was working: I changed `[H=application/x-httpd-php]` to `[T=image/jpg]` and now the file icon shows as an image as expected, but when I click it the server is still sending back a text/html file (in this case it should send back an image I imagine so). – jck Nov 26 '15 at 09:51
  • @Jck i think you need to add **5** after **php** in **httpd-php]** line, change it to **httpd-php5** or try [T=application/x-httpd-php] – Amit Verma Nov 26 '15 at 10:02
  • 2
    Great! It worked with **x-httpd-php5**, Thank you very much _( [T=application/x-httpd-php] didn't work, still shows the file as html in Firefox, or make the file to download in Microsoft Edge)_ – jck Nov 26 '15 at 10:17
0

According to Apache, you should not be using .htaccess: https://httpd.apache.org/docs/current/howto/htaccess.html#when

For the answer to your question, read my post here: https://stackoverflow.com/a/59868481/10664600

sudo vim /etc/httpd/conf/httpd.conf
Adam Winter
  • 1,680
  • 1
  • 12
  • 26
  • How do you know that the user has the permission to do this? Apache says specifically that `.htaccess` is for the times when the user doesn't have permission to modify the main configuration. – stdunbar Jan 22 '20 at 22:43
  • The title of this question implies root access, IMO, since one would probably not say "in Apache" if they were on a shared host. Nevertheless, one should still read the link, and, moreover, the answer I provided in the link still applies, as you would add the same thing to either the .htaccess file or the .conf file. – Adam Winter Jan 23 '20 at 21:42