如何用.htaccess中的斜杠替换破折号

时间:2011-12-30 03:15:32

标签: .htaccess mod-rewrite replace

我想替换PHP文件名中的所有短划线,但我不知道该怎么做。

基本上我有类似的东西:

http://localhost/category-activity.php

我想最终得到这个:

http://localhost/category/activity

我还需要脚本扫描所有短划线,这意味着如果我有类似的东西:

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

结束于:

http://localhost/category/activity/subactivity

我已使用以下代码删除扩展程序

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

1 个答案:

答案 0 :(得分:0)

试试这个:

# 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将继续重新应用规则,直到URI不被重写为止,因此像/category/activity/subactivity这样的uri将继续被重写,直到它完成所有未经修改的规则。