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

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

提前致谢。亲切的问候,M

4

1 回答 1

2

只需替换这个:

\w

... 有了这个:

[\w-]

Apache 的 mod_rewrite 基本上遵循 Perl 正则表达式语法。

于 2012-11-09T09:05:13.637 回答