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

发布于 2024-08-23 20:46:20 字数 2652 浏览 10 评论 0 原文

我正在尝试部署一个由 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.htaccess。 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 重写指南

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

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

我也有臭名昭著的“尾随斜杠问题”,这是通过设置 RewriteBase 解决的(就像以前那样)在下面的回复之一中建议):

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

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

I'm trying to deploy a site generated with Jekyll and would like to keep the site in its own subfolder on my server to keep everything more organized.

Essentially, I'd like to use the contents of /jekyll as the root unless a file similarly named exists in the actual web root. So something like /jekyll/sample-page/ would show as http://www.example.com/sample-page/, while something like /other-folder/ would display as http://www.example.com/other-folder.

My test server runs Apache 2.2 and the following .htaccess (adapted from http://gist.github.com/97822) works flawlessly:

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

However, my production server runs Apache 1.3, which doesn't allow DirectorySlash. If I disable it, the server gives a 500 error because of internal redirect overload.

If I comment out the last section of ReWriteConds and rules:

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !/$
RewriteRule ^(.*)$ $1/

…everything mostly works: http://www.example.com/sample-page/ displays the correct content. However, if I omit the trailing slash, the URL in the address bar exposes the real internal URL structure: http://www.example.com/jekyll/sample-page/

What is the best way to account for directory slashes in Apache 1.3, where useful tools like DirectorySlash don't exist? How can I use the /jekyll/ directory as the site root without revealing the actual URL structure?

Edit:

After a ton of research into Apache 1.3, I've found that this problem is essentially a combination of two different issues listed at the Apache 1.3 URL Rewriting Guide.

I have a (partially) moved DocumentRoot, which in theory would be taken care of with something like this:

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

I also have the infamous "Trailing Slash Problem," which is solved by setting the RewriteBase (as was suggested in one of the responses below):

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

The problem is combining the two. Moving the document root doesn't (can't?) use RewriteBase—fixing trailing slashes requires(?) it… Hmm…

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌︼了一个春 2024-08-30 20:46:20

经过一周的尝试,终于搞定了。 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。神奇的是,一切都有效。

Finally got it, after a week of trying. RewriteRules really are voodoo…

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]

No need for DirectorySlash. It magically all works.

兔姬 2024-08-30 20:46:20

您可以简单地使用:

RewriteBase   /jekyll

一切都从那一点开始。

You can simply use:

RewriteBase   /jekyll

And everything starts from that point.

2024-08-30 20:46:20

对我有用的唯一简单的解决方案是此处

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) /folder/$1 [L]

将此代码放入您的 .htaccess 文件中。

domain-name.com – 输入您自己的域名

文件夹 - 输入包含测试/开发网站的子文件夹的名称

The only simple solution worked for me is here

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) /folder/$1 [L]

Put this code into your .htaccess file.

domain-name.com – Type your own domain name

folder – Type the name of the subfolder which has the test/development website

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文