.htaccess:删除.html并添加一个尾部斜杠

时间:2015-09-07 01:19:44

标签: html .htaccess mod-rewrite

我当前的.htaccess文件是:



## Go Daddy servers need the Options -MultiViews code below
Options -MultiViews
RewriteEngine On
RewriteBase /

# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1/ [L,R=301]

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

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




但是当我访问某个页面时,它会显示"在此服务器上找不到请求的URL / somepage /。"我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

试试这个:

Options -Multiviews
RewriteEngine On
RewriteBase /

#Remove .html extension
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ %1/ [NC,L,R]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.html [NC,L]

此规则从文件中删除html扩展名,并在文件名末尾添加一个缩写斜杠。

在Apache 2.4上测试

答案 1 :(得分:0)

completeTransition

它适用于CSS和JS等资产。也可以使用斜杠结尾。