【问题标题】:Use a subdirectory as root with htaccess in Apache 1.3在 Apache 1.3 中使用 htaccess 以 root 身份使用子目录
【发布时间】:2011-01-25 00:28:42
【问题描述】:

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

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

编辑:

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

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

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

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

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

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

【问题讨论】:

  • 这太棒了。如果我有多个域指向服务器,有没有办法可以有条件地应用上述规则?因此,如果来自 domainA.com 的请求则公开 /domainA/ 子文件夹,就好像它是根目录一样。如果来自 domainB.com 的请求,那么公开子目录 /domainB/ 就好像它是根目录一样?
  • @bkwdesign 使用 nginx,这种重定向更简单。

标签: apache .htaccess jekyll subdirectory


【解决方案1】:

唯一对我有用的简单解决方案是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]

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

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

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

【讨论】:

    【解决方案2】:

    经过一周的努力,终于搞定了。 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。神奇地一切正常。

    【讨论】:

    • 完美。谢谢。
    • 我花了好几个小时才找到这个解决方案,但它很完美,到目前为止在任何地方都可以使用!非常感谢。
    • 还有一件事:但是我怎样才能让这个强制 https?我把下面的代码放在前面,但我不确定这是否是一个好的解决方案:RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]
    【解决方案3】:

    你可以简单地使用:

    RewriteBase   /jekyll
    

    一切都从那时开始。

    【讨论】:

    • 这个答案更好!!
    • 这个在我的主机上不适合我,上面的答案可以。
    • 如果您在主页 url 中有其他服务(如缩短器),这是否有效?第一个答案确实如此,所以只是想知道
    猜你喜欢
    • 2012-06-19
    • 2019-05-16
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多