Keith Devens .com |
Sunday, October 12, 2008 | ![]() |
| A language that doesn't affect the way you think about programming, is not worth knowing. – Alan Perlis | ||
|
| ← Nevow | Testing a comment change → |

Patrick (http://plog.grauhirn.org) wrote:
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 (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 (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
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.
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.
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.
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):
Generated in about 0.216s.
(Used 8 db queries)

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.