apache - 在 Apache 1.3 中使用 htaccess 作为根目录

标签 apache .htaccess jekyll subdirectory

我正在尝试部署一个由 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/

在 Apache 1.3 中,考虑目录斜杠的最佳方法是什么?在 Apache 1.3 中,不存在像 DirectorySlash 这样有用的工具?如何使用 /jekyll/ 目录作为站点根目录而不泄露实际的 URL 结构?

编辑:

经过对 Apache 1.3 的大量研究,我发现这个问题本质上是 Apache 1.3 URL Rewriting Guide 中列出的两个不同问题的组合。 .

我有一个(部分)移动的 DocumentRoot,理论上可以通过如下方式处理:

RewriteRule   ^/$  /e/www/  [R]

我还遇到了臭名昭著的“尾随斜杠问题”,该问题可以通过设置 RewriteBase 来解决(如下面的响应之一所建议):

RewriteBase    /~quux/
RewriteRule    ^foo$  foo/  [R]

问题在于将两者结合起来。移动文档根目录不(不能?)使用 RewriteBase - 修复尾部斜杠需要(?)它......嗯......

最佳答案

经过一周的尝试,终于成功了。 RewriteRules 确实是巫毒……

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 within jekyll
# This does not expose the internal URL.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^jekyll/(.*[^/])$ http://www.example.com/$1/ [R=301]

不需要DirectorySlash。它神奇地起作用了。

关于apache - 在 Apache 1.3 中使用 htaccess 作为根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2402140/

相关文章:

jekyll/css 不适用于 github 页面,但它适用于本地主机 :3000

markdown - Jekyll,最小值 : Using markdown for the homepage

ruby - Jekyll:在不使用外部插件的情况下获取图像的宽度/高度

Apache 拒绝 <Location> 但允许子位置

php - PHP 的 APC 设置不正确?

apache - elasticsearch和solr之间的根本区别是什么?

.htaccess - htaccess - 重定向查询字符串并将其从 URL 中删除

apache - 当 Apache Web 服务器使用 mod_jk 连接 Tomcat 时启用 SSL

linux - .htaccess 文件在 Redhat Linux Server V6.1 中不可见

php - 当我尝试使用 PHP 对 .js 文件进行 GZIP 压缩时,出现了某种与 PHP 相关的错误