一个非常奇怪的.htaccess问题

时间:2016-11-04 13:43:19

标签: html .htaccess trailing-slash

我们说我的网站是 www.example.com ,我在根目录中有一个 contact.html 页面。通常显示在地址栏中的网址为: http://example.com/contact.html

对于我的网站,我希望网址为:

  1. 没有 www
  2. 没有 .html
  3. 一个尾部斜杠

因此,最终结果应如下所示: http://example.com/contact/

条件1是默认值,因此不会打扰我。

对于条件2(没有' .html'),我将以下代码放入根目录中的.htaccess文件位置:

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

......而且效果很好。

现在进入条件3(强制执行尾部斜杠),我在<条件2'之前的.htaccess文件中有以下代码:重写命令

RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

... 工作;该页面重定向到错误404页面,地址栏显示:http:/example.com/404.html ... YES,带有斜杠:/

我做错了什么?

我在根目录中有一个名为 archives 的文件夹,我想要完成索引编制。所以我只是在'档案中添加了一个.htaccess文件。文件夹并添加了以下简单代码:

Options +Indexing

但它不起作用;我被重定向到Error 403页面!我99%确定它是由于 root 目录中的.htaccess文件。

1 个答案:

答案 0 :(得分:1)

尝试:

RewriteEngine On

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.+) %{REQUEST_URI}/ [R=301,L]

# remove html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1/ [R=301,L]

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