3

I want to replace all the dashes from my PHP filenames but I don't know how to do it.

Basically I have something like this:

http://localhost/category-activity.php

And I want to end up with this:

http://localhost/category/activity

I also need that the script scans for ALL dashes meaning that if I have something like:

http://localhost/category-activity-subactivity.php

Ends up in something like:

http://localhost/category/activity/subactivity

I am already using the following code to remove the extension

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Cœur
  • 37,241
  • 25
  • 195
  • 267
Agustin
  • 31
  • 2

1 Answers1

0

Try this:

# not an existing directory and is a php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
# make sure there are no slashes and doesn't already end with .php
RewriteCond %{REQUEST_URI} !^/.+/.+
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php  [L]

# not an existing directory or file (this needs to take place so existing paths won't get / replaced with -
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)$ /$1-$2   [L]

Mod-rewrite will continue to reapply rules until the URI doesn't get rewritten, so uri's like /category/activity/subactivity will continue to get rewritten until it goes through all the rules untouched.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks for the help, but I can't seem to get it working. I've made a link to a PHP file but the URL shows the full name + extension.I also tried tried to link without the extension or using slashes instead of dashes but it gets redirected to my index.php always. – Agustin Dec 30 '11 at 14:02