mod_rewriteが見つからない場合はすべて書き換え

mod_rewriteが見つからない場合はすべて書き換え

いくつかの書き換えルールを作成する必要がありますが、数年間Apache HTTPを使用していないため、ルールは少し混乱しています。

仕事:

  1. 次のURLを書き直してくださいhttp://example.com/test/file.jshttp://example.com/test/1/file.js
  2. ファイルが見つからない場合http://example.com/test/1/file.js- ファイルを返しますhttp://example.com/test/1/index.html

私は考えた - それは次のようになります:

    <Location /test>
            AllowOverride All

            # if file http://example.com/test/1/file.js found - return it
            RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -f
            RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/test/1/$1 [L]

            # if file http://example.com/test/1/file.js NOT found - return index.html form /1/ subdir
            RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/test/1/index.html [L]

    </Location>

コンテンツ/test/:

# ls -l
total 16
drwxr-xr-x 2 root root 4096 Aug 20 10:14 1
-rw-r--r-- 1 root root    6 Aug 20 08:57 index.html
-rw-r--r-- 1 root root    3 Aug 20 10:06 jj.ll
-rw-r--r-- 1 root root   64 Aug 20 08:50 test.html

そして/test/1/

# ls -l 1
total 12
-rw-r--r-- 1 root root 15 Aug 20 11:02 file.js
-rw-r--r-- 1 root root 13 Aug 20 10:03 index.html
-rw-r--r-- 1 root root  7 Aug 20 10:14 jj.ll

しかし、同様のURLを開こうとすると、http://example.com/test/file.jsとにかくファイルを受け取ります。http://example.com/test/1/index.html

ベストアンサー1

次のように解決できます。

            RewriteCond %{REQUEST_URI} !/test/1
            RewriteRule ^.*test(.*) https://%{HTTP_HOST}/test/1$1 [L]

            RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -f
            RewriteRule ^.*test(.*) %{DOCUMENT_ROOT}/test/1/$1 [L]

            RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
            RewriteRule ^.*test(.*) %{DOCUMENT_ROOT}/test/1/index.html [L]

うまくいくようです。

おすすめ記事