Remove .php extention and add a slash htaccess

Hi,

I have a website where most files have a php extention, and want to remove the exention and add a slash using htaccess.

So for example instead of
http://mydomain.com/test.php

I’d like…
http://mydomain.com/test/

I’ve tried loads of scripts from the web. They seem to work in that they remove the .php, but kept getting a 404. So I removed the .php extention from one of the files so it didn’t get a 404 but the file wasn’t then treated as a php file and showed the php code in the browser.

RewriteEngine on

RewriteCond %{THE_REQUEST} [1]{3,9}\ /(.*)\.php\ HTTP/ [NC]
RewriteRule .+ http://www.mydomain.com/%1 [R=301,QSA]

Can anyone tell me what I’m doing wrong?

Thanks!


  1. A-Z ↩︎

boog,

Yes:

  1. You’re using mod_rewrite to do your job (coding links) as webmaster, that is, you’re going the wrong way! Use mod_rewrite to ADD the .php extension to extensionless URIs.

  2. By telling Apache that your links are subdirectories when they’re really extensionless filenames makes everything overly complicated (and results in a directory level offset which will make your relative links worthless).

You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK