删除扩展并在静态html上添加斜杠

时间:2011-12-16 09:03:36

标签: .htaccess mod-rewrite redirect

我的root目录包含一些.html文件。 我想在地址栏中删除.html的扩展名,并在最后添加斜杠,如下所示:http://domain.com/page/ 我尝试过很多解决方案,其中之一就是:

RewriteEngine on
RewriteBase /

# redirect from ww to non-www
RewriteCond %{HTTP_HOST} ^www.nightmaar.com$ [NC] 
RewriteRule ^(.*)$ http://nightmaar.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.html [L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule .*[^/]$ $0/ [L,R=301]

但浏览器出错(重定向太多)。当我试图访问我的静态html页面时,没有从page.html到/ page /的重定向。

请帮我解决这个问题:S

1 个答案:

答案 0 :(得分:2)

你几乎做到了!

以下是应该起作用的:

RewriteEngine On
RewriteBase /

# redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www.nightmaar.com$ [NC] 
RewriteRule ^(.*)$ http://nightmaar.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.html [L]

# Remove trailing slash:
RewriteRule (.*)/$ $1 [L]

# Now test without the trailing slash:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule . %{REQUEST_FILENAME}.html [QSA,L]