【发布时间】:2011-01-25 00:28:42
【问题描述】:
我正在尝试部署一个使用 Jekyll 生成的网站,并希望将该网站保留在我的服务器上自己的子文件夹中,以使一切更有条理。
基本上,我想使用 /jekyll 的内容作为根,除非在实际的 Web 根中存在类似名称的文件。所以像/jekyll/sample-page/ 这样的东西会显示为http://www.example.com/sample-page/,而像/other-folder/ 这样的东西会显示为http://www.example.com/other-folder。
我的测试服务器运行 Apache 2.2 并且以下 .htaccess(改编自 http://gist.github.com/97822)完美运行:
RewriteEngine On
# Map http://www.example.com to /jekyll.
RewriteRule ^$ /jekyll/ [L]
# Map http://www.example.com/x to /jekyll/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/jekyll/
RewriteRule ^(.*)$ /jekyll/$1
# Add trailing slash to directories without them so DirectoryIndex works.
# This does not expose the internal URL.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !/$
RewriteRule ^(.*)$ $1/
# Disable auto-adding slashes to directories without them, since this happens
# after mod_rewrite and exposes the rewritten internal URL, e.g. turning
# http://www.example.com/about into http://www.example.com/jekyll/about.
DirectorySlash off
但是,我的生产服务器运行 Apache 1.3,它不允许 DirectorySlash。如果我禁用它,由于内部重定向过载,服务器会给出 500 错误。
如果我注释掉 ReWriteConds 和规则的最后一部分:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !/$
RewriteRule ^(.*)$ $1/
...一切正常:http://www.example.com/sample-page/ 显示正确的内容。但是,如果我省略尾部斜杠,地址栏中的 URL 会暴露真实的内部 URL 结构:http://www.example.com/jekyll/sample-page/
在不存在 DirectorySlash 等有用工具的 Apache 1.3 中,解决目录斜杠的最佳方法是什么?如何在不暴露实际 URL 结构的情况下使用/jekyll/ 目录作为站点根目录?
编辑:
在对 Apache 1.3 进行大量研究后,我发现这个问题本质上是Apache 1.3 URL Rewriting Guide 中列出的两个不同问题的组合。
我有一个(部分)移动的 DocumentRoot,理论上可以这样处理:
RewriteRule ^/$ /e/www/ [R]
我还有臭名昭著的“尾随斜线问题”,可以通过设置 RewriteBase 来解决(正如以下回复之一所建议的那样):
RewriteBase /~quux/
RewriteRule ^foo$ foo/ [R]
问题在于将两者结合起来。移动文档根目录不(不能?)使用RewriteBase——修复尾部斜杠需要(?)它......嗯......
【问题讨论】:
-
这太棒了。如果我有多个域指向服务器,有没有办法可以有条件地应用上述规则?因此,如果来自 domainA.com 的请求则公开 /domainA/ 子文件夹,就好像它是根目录一样。如果来自 domainB.com 的请求,那么公开子目录 /domainB/ 就好像它是根目录一样?
-
@bkwdesign 使用 nginx,这种重定向更简单。
标签: apache .htaccess jekyll subdirectory