Apache 1.3 で htaccess を使用してサブディレクトリを root として使用する
-
18-09-2019 - |
質問
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/
Apache 1.3 でディレクトリ スラッシュを考慮する最善の方法は何ですか。 DirectorySlash
存在しないのですか?どうすれば使えますか /jekyll/
実際の URL 構造を明らかにせずに、ディレクトリをサイトのルートとして使用しますか?
編集:
Apache 1.3 について大量の調査を行った結果、この問題は本質的に、 Apache 1.3 URL 書き換えガイド.
(部分的に)移動されたDocumentRootがありますが、理論的には次のようなもので処理されます。
RewriteRule ^/$ /e/www/ [R]
また、悪名高い「末尾のスラッシュ問題」も抱えていますが、これは次のように設定することで解決できます。 RewriteBase
(以下の回答のいずれかで示唆されているように):
RewriteBase /~quux/
RewriteRule ^foo$ foo/ [R]
問題は、この 2 つを組み合わせることです。ドキュメントルートの移動では、 RewriteBase
—末尾のスラッシュを修正するには必要(?)…うーん…
解決
1週間試した後、ようやく入手できました。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 ファイルに追加します。