.htaccess有或没有斜线

时间:2009-02-12 08:31:07

标签: .htaccess mod-rewrite rewrite

我需要对以下重写规则做些什么来使其无论是否在URL的末尾都是斜杠?

即。 http://mydomain.com/content/featured 要么 http://mydomain.com/content/featured/

RewriteRule ^content/featured/ /content/today.html 

2 个答案:

答案 0 :(得分:34)

使用$标记字符串的结尾,使用?标记前面的表达式重复零次或一次:

RewriteRule ^content/featured/?$ content/today.html

但我建议你坚持使用一种符号并纠正错误拼写:

# remove trailing slashes
RewriteRule (.*)/$ $1 [L,R=301]

# add trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ $0/ [L,R=301]

答案 1 :(得分:3)

这样做的简单方法:

RewriteEngine On
RewriteBase / 
RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC]