Mod_rewrite to replace extensions?
August 13, 2008 1:15 AM
Subscribe
I need to figure out how to use mod_rewrite to trim the extensions of all of a certain type of file (.php) and replace them with a forward slash
I need to figure out how to use mod_rewrite to trim the extensions of all of a certain type of file (.php) and replace them with a forward slash. I've been hunting on the net for information and poking around in the regex docs, but this one still escapes me.
I want my sites files that look like
"www.domain.com/page.php"
to look like
"www.domain.com/page/"
I know I can write RewriteRules to do that to individual pages, but how do I make it work on all without updating the .httaccess?
posted by SECONDHANDSMOTE to computers & internet (4 comments total)
3 users marked this as a favorite
The $ at the end of php anchors the pattern comparison to the end of the URL -- basically, if the URL ends in .php, it will match and apply the rule.
Note, this will also replace URLs like:
example.com/website/subdirectory/foo.php
with
example.com/website/subdirectory/foo/
You may want to be somewhat more restrictive in your rule, perhaps matching only filenames in that directory. For that, you'd use...
RewriteRule ([a-zA-Z0-9_]+)\.php$ $1\/
posted by cheaily at 2:00 AM on August 13, 2008 [2 favorites]