mip*_*adi 7 rewrite apache-2.2
我正在使用 Apache 为一个由静态 HTML 文件组成的博客提供服务。目前,该博客使用非常标准的 URL 结构,如下所示:
/2010/03/21/my-awesome-blog-post/
Run Code Online (Sandbox Code Playgroud)
映射到文件
/2010/03/21/my-awesome-blog-post/index.html
Run Code Online (Sandbox Code Playgroud)
使用 Apache 的mod_dir.
我想删除尾部斜杠,以便 URL 像
/2010/03/21/my-awesome-blog-post
Run Code Online (Sandbox Code Playgroud)
以相同的方式工作(并且不会被重定向)。有没有办法用 Apache 做到这一点?
(请注意,我希望带有斜杠的URL 也能继续工作。)
(进一步说明:我看到了一些关于 ApacheDirectorySlash指令的内容,但我认为它没有做我想要的……虽然我不确定。)
Axe*_*auf 12
Apache 重写指南有一章关于“尾随斜杠问题”(向下滚动一点)解释了如何解决尾随斜杠问题。
但是,他们还指出,当其中呈现的页面使用具有相对链接的资源(图像、样式表等)时,目录需要尾部斜杠 - 没有斜杠将无法工作:
“这个微妙问题的解决方案是让服务器自动添加尾部斜杠。为了正确地做到这一点,我们必须使用外部重定向,因此浏览器正确请求后续图像等。如果我们只进行内部重写,这只会适用于目录页面,但是当任何图像包含在具有相对 URL 的页面中时会出错,因为浏览器会请求一个内联对象。例如,请求 /~quux/foo/index 中的 image.gif .html 将成为 /~quux/image.gif 没有外部重定向!”
小智 7
您始终可以使用 mod_rewrite 将目录名称重定向到dirname/index.html. 您可以使用 RedirectConds 来确保在 URL 以尾部斜杠或 .html 结尾时重定向不会完成,并且它仅适用于博客文章 URL。
让我举一个例子,这需要一点时间。
# Trailing slashes and .html suffix
RewriteCond !/$
RewriteCond !\.html$
# Check if it's actually a dir and if index.html exists
RewriteCond %{REQUEST_URI} -d
RewriteCond %{REQUEST_URI}/index.html -f
# Rewrite anything that gets through (Probably insecure, but you get the idea)
RewriteRule ^(.*)$ $1/index.html
Run Code Online (Sandbox Code Playgroud)
编辑:也可以结合 Matt 的解决方案,将重定向错误代码添加到 RewriteRule。它可能也应该成为最后一个 RedirectRule。有关更多信息,请参阅mod_rewrite 文档。
| 归档时间: |
|
| 查看次数: |
33291 次 |
| 最近记录: |