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



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

有人有一个想法在其中处理标题( - )吗?

预先感谢。亲切的问候,M

只需替换:

w

...与此:

[w-]

Apache的Mod_rewrite基本上遵循Perl Regexp语法。

最新更新