在 Apache 1.3 中使用 htaccess 作为根目录使用子目录 [英] Use a subdirectory as root with htaccess in Apache 1.3

查看:27
本文介绍了在 Apache 1.3 中使用 htaccess 作为根目录使用子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试部署一个用 Jekyll 生成的网站,并希望将该网站保存在我服务器上的自己的子文件夹中,以使一切更有条理.

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.

本质上,我想使用 /jekyll 的内容作为根目录,除非实际的 web 根目录中存在类似名称的文件.所以像 /jekyll/sample-page/ 这样的东西会显示为 http://www.example.com/sample-page/,而类似 /other-folder/ 的内容会显示为 http://www.example.com/other-folder.

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.

我的测试服务器运行 Apache 2.2 和以下 .htaccess(改编自 http://gist.github.com/97822) 完美运行:

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

但是,我的生产服务器运行 Apache 1.3,它不允许 DirectorySlash.如果我禁用它,由于内部重定向过载,服务器会给出 500 错误.

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.

如果我注释掉 ReWriteConds 和规则的最后一部分:

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

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

...一切正常:http://www.example.com/sample-page/ 显示正确的内容.但是,如果我省略尾部斜杠,地址栏中的 URL 会暴露真实的内部 URL 结构:http://www.example.com/jekyll/sample-page/

…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/

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

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?

在对 Apache 1.3 进行大量研究后,我发现这个问题本质上是 Apache 1.3 URL 重写指南.

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.

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

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

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

我还有臭名昭著的尾随斜线问题",它可以通过设置 RewriteBase 来解决(正如以下回复之一所建议的那样):

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]

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

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

推荐答案

经过一周的尝试,终于搞定了.RewriteRules 真的是巫术……

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]

不需要DirectorySlash.它神奇地一切正常.

No need for DirectorySlash. It magically all works.

这篇关于在 Apache 1.3 中使用 htaccess 作为根目录使用子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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