【问题标题】:.htaccess Rewrite .html - One page keeps giving 403 Forbidden error.htaccess 重写 .html - 一页不断给出 403 Forbidden 错误
【发布时间】:2017-05-22 20:21:48
【问题描述】:

我正在尝试隐藏我网站的 .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 文件,位于根文件夹中,就像联系人和 about 一样。但有来自子文件夹的链接(其中包含博客页面 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

【问题讨论】:

  • 您遇到了与目录斜线相关的问题。使用 htaccess DirectorySlash off 中的以下行关闭目录斜杠

标签: html apache .htaccess


【解决方案1】:

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

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

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

/blog/.htaccess

 # Note there is no .html as this is removed by the 
 # htaccess in the folder above. 
 DirectoryIndex post1

Read more about this here.

Abhishek gurjar's answer 还为您提供了有关如何改进当前.htacess 规则的详细信息。但是他的解决方案不能解决您禁止访问/blogs/ 文件夹索引的问题。

【讨论】:

  • martin ,htaccess 在根目录下。如果 op 在 blog 文件夹中创建了一个新的 htaccess,那么规则不会删除 blog lavel 请求的扩展。
  • DirectoryIndex post1 会工作吗? @starkeen 我已经更新了我的答案
【解决方案2】:

试试这个规则,

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

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

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

在规则上方检查请求的 url 是否是实际的 html 文件,而不是执行规则。否则,如果它是一个目录,那么它服务于该目录。

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2011-08-09
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多