View Full Version : Friendly static php urls

06-12-2011, 06:30 PM
I have read many articles on making friendly URLs but I can't seem to get it working properly.

My site is hosted on a Linux box with Apache.

All I want to do is drop the file extensions so /hello.php shows as /hello

For the purposes of setting this up, I am working with a simple hello world file.

Echo "Hello, World!";
?> [code]

First I saved the file as "hello.php". When I navigate to mysite.com/hello.php the page appears normally.

Next, I added the following to my .htaccess

[code]<Files hello>
ForceType application/x-httpd-php

My desire is to leave my files in their current format on the server, but simply drop the extension in the URL. I couldn't find a way to do such, so I renamed the file from "hello.php" to "hello". When I navigate to mysite.com/hello I get a prompt asking if I wish to open or save the file. If I choose to open it, I see a text document with the code the same as I would if I right-clicked and chose View Page Source.

I simply wish the file to open and display normally.

The .htaccess file is in the same directory as the php file.

07-09-2011, 09:25 AM
Personally I'd just use mod_rewrite to redirect all requests for extensionless paths that don't exists to .php...

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z_-]+)$ $1.php [L]