2012-11-09 18 views
2

我已經想出了處理PHP文件,目錄下面的重寫規則:如何在重寫規則中處理連字符( - )標誌?

www.domain.com/name.php -> www.domain.com/name/ 
www.domain.com/name -> www.domain.com/name/ 
www.domain.com/name/ -> www.domain.com/name.php 

上面的東西的工作,然而,當標題有一個連字符( - )符號在裏面,那麼它不會添加拖車斜線結束,它將最終成爲一個404頁面。 示例不工作:

www.domain.com/stack-overflow -> 404 page 
www.domain.com/stack-overflow/ -> 404 page 

當前重寫代碼:

# 404 page 
ErrorDocument 404 /404.php 

# Add automatic a trailer slash on the end 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !# 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ $1/ [L,R=301] 

# Decline direct access to .php files, redirect .php to name/ 
RewriteCond %{ENV:REDIRECT_STATUS} !200 
RewriteRule ^(\w+)\.php$ $1/ [R=301] 
# name/ to the right .php file 
RewriteRule ^(\w+)/?$ /$1.php 

有別人的想法來處理職稱連字符( - )的呢?

在此先感謝。 親切的問候, 中號

回答

2

只需更換這一點:

\w 

...這一點:

[\w-] 

Apache的mod_rewrite基本上沿用Perl的正則表達式的語法。

+0

太棒了,那個伎倆!非常感謝。 –