在Apache 1.3中使用一个子目录根与htaccess的
-
18-09-2019 - |
题
我试图部署具有杰基尔生成的网站,并希望保持网站在我的服务器上自己的子文件夹把一切都更有条理。
基本上,我想使用/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/
什么是占目录的Apache 1.3,斜线,其中有用的工具,如DirectorySlash
不存在最好的方法是什么?我如何使用/jekyll/
目录作为网站的根没有透露实际的URL结构?
修改强>
一吨的研究的Apache 1.3之后,我发现这个问题本质上是在的阿帕奇1.3 URL重写指南。
我有一个(部分地)移动的DocumentRoot,这在理论上将与像这样被照顾:
RewriteRule ^/$ /e/www/ [R]
我也有臭名昭著的“结尾的斜线问题”,这是通过设置RewriteBase
解决(如在下面的反应中的一个建议):
RewriteBase /~quux/
RewriteRule ^foo$ foo/ [R]
的问题是两者结合。移动文档根不(不能?)使用RewriteBase
定影拖尾斜线需要(?)它...嗯...
解决方案
最后得到它,尝试一个星期后。的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
。它神奇的所有作品。
其他提示
可以简单地使用:
RewriteBase /jekyll
和一切从该点开始。
在只有简单的解决方案为我的这里
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文件。