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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2402140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:51:53  来源:igfitidea点击:

Use a subdirectory as root with htaccess in Apache 1.3

apache.htaccessjekyllsubdirectory

提问by Andrew

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 生成的站点,并希望将该站点保存在我服务器上的自己的子文件夹中,以使所有内容更有条理。

Essentially, I'd like to use the contents of /jekyllas 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.

本质上,我想使用 的内容/jekyll作为根目录,除非实际的 web 根目录中存在类似命名的文件。所以类似的内容/jekyll/sample-page/会显示为http://www.example.com/sample-page/,而类似的内容/other-folder/会显示为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:

我的测试服务器运行 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/

# 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 ^(.*)$ /

# 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.

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

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

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

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

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

……一切正常:http: //www.example.com/sample-page/显示正确的内容。但是,如果我省略尾部斜杠,地址栏中的 URL 会暴露真实的内部 URL 结构: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 DirectorySlashdon't exist? How can I use the /jekyll/directory as the site root without revealing the actual URL structure?

在 Apache 1.3 中考虑目录斜杠的最佳方法是什么,其中DirectorySlash不存在有用的工具?如何/jekyll/在不透露实际 URL 结构的情况下使用该目录作为站点根目录?

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.

在对 Apache 1.3 进行大量研究后,我发现这个问题本质上是Apache 1.3 URL 重写指南 中列出的两个不同问题的组合。

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

我有一个(部分)移动的 DocumentRoot,理论上可以用这样的东西来处理:

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(如下面的其中一个回复中所建议的)来解决的:

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…

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

回答by Andrew

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

经过一周的努力,终于搞定了。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/

# Add trailing slash to directories within jekyll
# This does not expose the internal URL.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^jekyll/(.*[^/])$ http://www.example.com// [R=301]

No need for DirectorySlash. It magically all works.

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

回答by Marcos Placona

You can simply use:

您可以简单地使用:

RewriteBase   /jekyll

And everything starts from that point.

而一切都从那一刻开始。

回答by Umair Hamid

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/ [L]

Put this code into your .htaccess file.

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