.htaccess显示目录index.html,不带斜杠 [英] .htaccess to show a directory index.html without a trailing slash

查看:237
本文介绍了.htaccess显示目录index.html,不带斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Apache服务器上运行了Jekyll生成的网站,但是在正确设置.htaccess文件时遇到了一些麻烦。 Jekyll将index.html文件放入代表每个页面的文件夹中,因此我的URL当前看起来像 domain.com/foo /

I've got a Jekyll generated site running on an Apache server and I'm having some trouble getting my .htaccess file set up correctly. Jekyll places index.html files into folders which represent each page so my URLs currently look like domain.com/foo/

我想从URL中删除该斜杠,使其与我之前设置的完全匹配(也是因为我认为它看起来更好)。

I'd like to remove that trailing slash from the URL so that it exactly matches what I had set up previously (and also because I think it looks better).

当前,我的.htaccess文件中处理重载的部分如下:

Currently the section of my .htaccess file dealing with rewites looks like:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>

Options -Indexes

DirectoryIndex index.xml index.html

我尝试按照建议此处但这会使我进入重定向循环。

I have tried following the advice here but that puts me into a redirect loop.

有人可以帮助我吗?简而言之,我想要的是让domain.com/foo URL显示/ foo目录中的index.html文件,以及让domain.com/foo/和domain.com/foo/index.html重定向到domain。 com / foo。

Can anybody help me out? In brief, what I want is for a domain.com/foo URL to show the index.html file form the /foo directory and for domain.com/foo/ and domain.com/foo/index.html to redirect to domain.com/foo.

推荐答案

您应该可以使用它来关闭斜杠的添加。

You should be able to use this to turn off the addition of slashes.

DirectorySlash Off

请注意,添加斜杠是有充分理由的。在目录名中包含斜杠将使相对URL指向同一事物,而不管URL是否以 foo / bar / index.html结尾 或 foo / bar /。如果没有结尾斜杠,则相对URL将引用比它们通常指向的目录高一级的内容。 (例如: baz.jpg将为用户提供 /foo/baz.jpg,而不是 /foo/bar/baz.jpg,因为尾随的 bar如果未受到因此,如果这样做,您可能要避免使用相对URL。

Note that the trailing slash is added for a good reason. Having the trailing slash in the directory name will make relative URLs point at the same thing regardless of whether the URL ends with "foo/bar/index.html" or just "foo/bar/". Without the trailing slash, relative URLs would reference something up one level from what they normally point at. (eg: "baz.jpg" would give the user "/foo/baz.jpg" instead of "/foo/bar/baz.jpg", as the trailing "bar" will get removed if it isn't protected by a trailing slash.) So if you do this, you probably want to avoid relative URLs.

然后重写目录名称以返回index.html,您可能可以做一些事情像这样:

To then rewrite the directory name to return the index.html you could probably do something like this:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}/index.html -f
RewriteRule ^(.*)$ /$1/index.html [L]

此命令检查REQUEST_URI / index.html是否存在,如果确实执行了内部重定向。

This checks if REQUEST_URI/index.html exists, and if it does performs an internal redirect.

这篇关于.htaccess显示目录index.html,不带斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆