2010-03-08 55 views
24

使用一個子目錄根與htaccess的我試圖部署具有傑基爾生成的網站,並希望保持網站在我的服務器上自己的子文件夾把一切都更有條理。Apache 1.3中

從本質上講,我想使用的/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不存在?如何在不透露實際URL結構的情況下將/jekyll/目錄用作網站根目錄?

編輯:

一噸的研究的Apache 1.3之後,我發現這個問題本質上是在Apache 1.3 URL Rewriting Guide上市兩個不同的問題的組合。

我有一個(部分)移動的DocumentRoot,這在理論上會與像這樣照顧:

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

我也有臭名昭著的「尾隨斜線問題,」這是通過設置解決RewriteBase(如有人建議在下面的反應中的一個):

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

問題是結合兩個。移動文檔根目錄沒有(?不能)使用RewriteBase - 定影結尾的斜槓需要它...嗯...

+0

這太棒了。如果我有多個域指向服務器,有沒有辦法以條件方式應用上述規則?因此,如果來自domainA的請求。com然後公開/ domainA /子文件夾就好像它是根..如果來自domainB.com的請求,然後暴露subdir/domainB /就像它是根? – bkwdesign 2014-03-08 14:48:10

+0

@bkwdesign使用nginx,這種重定向更簡單。 – 2014-10-28 06:50:00

回答

47

終於明白了,經過一週的嘗試。 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。它神奇地所有的作品。

+0

http://stackoverflow.com/a/9413539/3017716 – Ken 2017-04-03 18:14:27

10

您可以簡單地使用(?):

RewriteBase /jekyll 

所有的一切都從該點開始。

+0

這個回答比較好! – maxtorzito 2015-11-16 07:35:20

+0

這一個不適合我在我的託管,上面的答案呢。 – Ray1618 2016-12-23 14:51:27