重写一切不包括index.html

时间:2018-01-18 17:11:36

标签: .htaccess mod-rewrite

我有以下网站结构:

的index.html

/assets/subpage.html

/assets/secondpage.html

我在.htaccess中写了以下规则:

RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/assets
RewriteRule (.*) /assets/$1 [QSA,L]

我想从文件中删除.html(第一条规则),我想跳过/资产(第三条和第四条规则)。但是我想从3 i 4规则中排除.index.html。现在当我输入:

www.example.com它将我重定向到www.example.com/assets/.html(我希望这个将我重定向到www.example.com) 当我输入www.example.com/subpage时,它会将我重定向到:www.example.com/assets/subpage.html

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您可以使用:

DirectoryIndex index.html
RewriteEngine On

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

RewriteCond %{REQUEST_URI} !^/(index\.html$|assets/) [NC]
RewriteRule (.+) assets/$1 [L]
  • 在第一次规则检查中添加了RewriteCond,以确定是否存在.html文件
  • 使用.+确保我们与目标网页不匹配