KBD

Keith Devens .com

Sunday, October 12, 2008 Flag waving
A language that doesn't affect the way you think about programming, is not worth knowing. – Alan Perlis
← NevowTesting a comment change →

Daily link icon Thursday, May 20, 2004

How can I tell Apache things about files with no extensions?

Question: How can I tell Apache things about files with no extensions?

For example, I currently tell Apache to parse files with no extension as PHP by using a DefaultType setting. However, I just found out that files with extensions that Apache is unfamiliar with will also be interpreted as PHP. Is there any way to tell Apache to treat files with no extensions as PHP, but only files with no extensions?

Also, is it possible to query Apache somehow for all of its current settings so I can find them out without looking at httpd.conf?

← NevowTesting a comment change →

Comments XML gif

Patrick (http://plog.grauhirn.org) wrote:

Hi. As far as I know, Apache is able to work with regexpes in the settings. So it should be possible to tell him, that he should parse everything containing no dot [^\.] as php. The problem here is, what you define as an extension. If everything after a dot is an fileextension, this concept should work for you.

And apache has no option to show any configuration.

∴ Patrick | 20-May-2004 12:55am est | http://plog.grauhirn.org | #4632

walter (http://www.xanadb.com) wrote:

Keith,

Try adding the following to your .htaccess file...

RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9/_]+)$ $1.php

this should work for any filename that doesn't have '-' (dash) in it.

Walter

∴ walter | 20-May-2004 6:12am est | http://www.xanadb.com | #4634

walter (http://www.xanadb.com) wrote:

Keith,

correction to earlier comment. That RE should be...

RewriteRule ^([a-zA-Z0-9/_-]+)$ $1.php

This RE will work for filenames with dashes (-) in them too.

∴ walter | 20-May-2004 6:24am est | http://www.xanadb.com | #4635

walter (http://www.xanadb.com) wrote:

weird, there should be a backslash in front of the last '-' in the above RE, but your comment system must be stripping it.

3rd time lucky...

RewriteRule ^([a-zA-Z0-9/_\-]+)$ $1.php

∴ walter | 20-May-2004 6:28am est | http://www.xanadb.com | #4636

Sparticus (http://iamsparticus.co.uk) wrote:

Keith when you figure it out, could you publish the code? I want to get rid of all my .php extensions but couldn't work out how to do that but allow apache to parse them as php.
Ta.

∴ Sparticus | 20-May-2004 6:53am est | http://iamsparticus.co.uk | #4637

Ronaldo (http://reflectivesurface.com/weblog/) wrote:

I use the following rewrite rules:

RewriteCond %{REQUEST_FILENAME} /[^.]+$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [T=application/x-httpd-php,L]

The first line matches all files in which the basename contains no dots. The following line make sures the file is a real file, not a directory. And the last line just tells Apache consider the file's MIME type to be application/x-httpd-php, which is the MIME type associated with Apache. It has worked for me so far.

∴ Ronaldo | 20-May-2004 8:34am est | http://reflectivesurface.com/weblog/ | #4638

Keith (http://keithdevens.com/) wrote:

Ronaldo, that seems like it might be a great solution. Let me try that out.

Sparticus, this is code I currently have commented out of my .htaccess file that will do what you want:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_FILENAME}.php [L]

That way, you can leave .php extensions on your files, which will A. get them parsed by Apache as PHP, and B. still get you syntax highlighting when you open those files up in your edtior.

You'll probably want to automatically forward everything with a .php extension to a url without the .php extension as well. I had a super-hard time coming up with mod_rewrite rules for this. Mod_rewrite seems to just be completely broken every time I ever try to do anything with it. That's why I bypass mod_rewrite almost completely by sending all requests for non-existent files to a PHP program that can do whatever it wants:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . dispatcher.php

The way mod_rewrite was broken this time was that I tried to put a [L] on the end of the second rule immediately above (after the 'dispatcher.php'), but if I put rules after that it still ran them! The [L] seemed to have no effect whatsoever. I have no clue why. So, unfortunately I can't help you with the second part of the problem.

The way I do it for myself is through that dispatcher.php program. Go to http://keithdevens.com/programming.php and you'll see that it redirects you to just /programming. I store all my "content" in a special directory outside of my document root, partly to avoid cluttering up my public_html folder, and I have dispatcher.php serve it all. I can give you some of the code I use for that if you ask.

Keith | 23-May-2004 4:09am est | http://keithdevens.com/ | #4652

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 7 posts

Recent comments XML

I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.216s.

(Used 8 db queries)

mobile phone