如何在重写规则中处理连字符( - )符号?

时间:2012-11-09 07:41:31

标签: .htaccess mod-rewrite rewrite

我想出了以下重写规则,将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

有人有想法处理带有连字符( - )的标题吗?

提前致谢。 亲切的问候, 中号

1 个答案:

答案 0 :(得分:2)

只需替换它:

\w

......用这个:

[\w-]

Apache的mod_rewrite基本上遵循Perl regexp语法。

相关问题