使用一个子目录根与htaccess的Apache 1.3中 [英] Use a subdirectory as root with htaccess in Apache 1.3

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

问题描述

我想与部署杰基尔生成的网站,并希望保持网站在自己的子文件夹在我的服务器上把一切都更有条理。

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.

从本质上讲,我想使用的 /杰基尔的内容作为根,除非一个类似命名的文件存在于实际的Web根目录。因此,像 /哲基尔/样品页/ 将显示为 HTTP:/ /www.example.com/sample-page/ ,而像 /其他文件夹/ 将显示为的 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的/哲基尔/样品页/

…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 有用的工具是不存在的?我如何使用 /化身/ 目录站点根目录没有透露实际的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 -fixing尾随斜线需要(?)吧...嗯...

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

推荐答案

终于明白了,一个星期的努力之后。规则集真的是巫术......

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.

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

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