2011-12-30 38 views
3

我想從我的PHP文件名中替換所有破折號,但我不知道該怎麼做。如何在.htacess中用斜槓替換破折號

基本上,我有這樣的事情:

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 

回答

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] 

國防部重寫將繼續重新規則,直到URI不會被改寫,所以像/category/activity/subactivity這樣的uri將繼續被重寫,直到它經歷所有規則不變。

+0

感謝您的幫助,但我似乎無法得到它的工作。我已經建立了一個PHP文件的鏈接,但是該URL顯示了全名+擴展名。我也嘗試嘗試鏈接沒有擴展名或使用斜槓而不是破折號,但它總是被重定向到我的index.php。 – Agustin 2011-12-30 14:02:47