如何在删除html扩展名后添加斜杠

时间:2012-02-29 13:08:56

标签: .htaccess mod-rewrite url-rewriting rewrite slash

如何在删除html扩展名后使用以下代码添加/:

Options +FollowSymLinks -MultiViews
DirectorySlash off

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

1 个答案:

答案 0 :(得分:0)

您需要进行一些更改。

Options +FollowSymLinks -MultiViews

RewriteEngine On    
RewriteBase /

#Code to add forward slash
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(?:\.\w+|/)$
RewriteRule (.*) $1/ [R,L]

#To check whether a .html appended string is a file existing on the system 
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (.*)/ $1.html [L]

#### NOT REQUIRED
#RewriteCond %{SCRIPT_FILENAME}/ -d
#RewriteCond %{SCRIPT_FILENAME}.html !-f
#RewriteRule [^/]$ %{REQUEST_URI}/ [R,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R,L]

这些规则也不会按预期运作:

RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]+/$ %{REQUEST_URI}.html [QSA,L]

cos,%{REQUEST_URI}最后总是/。如果要求domain.com/about之类的URI,

它将被重写为:

domain.com/about/

最后到

domain.com/about/.html