.htaccess重写.html - 一页不断给出403 Forbidden错误

时间:2017-01-08 13:45:34

标签: html apache .htaccess

我正在尝试隐藏我网站的.html扩展名 我在StackOverflow上使用下面的代码来执行此操作

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

这适用于每个页面(about.html,contact.html都隐藏)期望blog.html

基本上,mydomain.com/blog给了我以下错误;

You don't have permission to access /blog/ on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

这是一个HTML文件,位于根文件夹中,就像联系人一样。但是有一个子文件夹(包含博客页面HTML)的链接。所以我猜这个问题来自那个?

另外作为附注,如果我直接访问博客文章(mydomain.com/blog/post1.html),它可以工作并隐藏.html

我搜索了很多但找不到解决方案。我该如何解决这个问题?

非常感谢。

    My document order

 - .htaccess
 - about.html
 - blog.html [only file that gives 403 error]
 - blog (folder)
      - post1.html
      - post2.html

2 个答案:

答案 0 :(得分:1)

首先;发生错误是因为没有.html您的博客页面与博客文件夹具有完全相同的标识符。 (由blog确定)
通过文件夹的首要文件,服务器然后打开/blog/文件夹,但博客文件夹没有索引页面,服务器(非常正确)拒绝目录列表访问。

这是您看到的错误 - 您的服务器阻止您查看/blog/文件夹中的文件列表。禁止的。

解决方案: - 将索引页添加到/blog/文件夹。这可以简单地重定向到博客文件夹中的任何页面(因此/blog/index.html加载/blog/post1.html)。 - 或者您可以将默认索引页面设置为存在的页面,例如blog1.html

<{1}}

中的

/blog/.htaccess

Read more about this here

Abhishek gurjar's answer为您提供有关如何改进当前 # Note there is no .html as this is removed by the # htaccess in the folder above. DirectoryIndex post1 规则的详细信息。但他的解决方案并未解决您对.htacess文件夹索引的禁止访问问题。

答案 1 :(得分:0)

尝试此规则,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([\w-]+)$ $1.html [QSA,L]

请在尝试此规则之前评论或删除之前的规则。

在您之前的规则中,您提出的条件是,如果请求既不是文件名也不是目录,而不是重写规则。但问题是你有博客作为一个目录,它对博客目录提出了确切的要求,我假设你已禁用目录列表,这就是禁止错误的原因。

以上规则检查请求的url是否是执行规则的实际html文件。否则,如果它是一个目录,那么它就会提供目录。