RewriteOptions Inherit
By default, mod_rewrite overrides rules when merging sections belonging to the same context. The RewriteOptions directive can change this behavior, for example using the Inherit setting.
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
デフォルトは同じコンテキストはオーバライド
この場合、全部のドメインで共通するようなURLの書き換え(mod_rewrite)は、www直下に.htaccessを置いて一つで済ませたいですが、下層ディレクトリに置いた.htaccessでもう一度RewriteEngine onしてしまうと親ディレクトリで設定したリライトが効かなくなってしまいます。
...
親ディレクトリで設定したURL書き換えを下層でも引き継ぐにはRewriteOptions Inheritを設定します。親ディレクトリのURL書き換え(mod_rewrite)を引き継ぐ ‹ レンタルサーバ ‹ emwaiblog 2016
例
<Directory "/foo"> RewriteEngine On RewriteRule testrule1 - [F] </Directory> <Directory "/foo/bar"> RewriteEngine On RewriteRule testrule2 - [F] RewriteOptions Inherit </Directory> <VirtualHost *:80> ServerName example.com DocumentRoot /foo RewriteEngine On RewriteRule testrule3 - [F] </VirtualHost>
RewriteOptions Inherit を指定 → testrule1 testrule2 testrule3 が有効
※ 指定なし(デフォルト)だと testrule2 testrule3 が有効
→ testrule1はオーバライドされ無効、VirtualHostはコンテキストが異なるので有効
※ .htaccess でも同じ
/foo/bar/.htaccess RewriteEngine On RewriteRule testrule2 RewriteOptions Inherit
処理順
つまり、ディレクティブに複数マッチする処理があった場合は、パスが短いものから順に処理されていくという話なので、最優先されるのは一番長いパスのものらしい。
セクションのマージ方法 マージの順番は以下のようになっています: 1. <Directory> (正規表現無し) と .htaccess を同時に (.htaccess が許可されていれば、それが <Directory> を上書きします) 2. <DirectoryMatch> と <Directory ~> 3. <Files> と <FilesMatch> を同時に 4. <Location> と <LocationMatch> を同時に <Directory> 以外は、それぞれのグループは設定ファイルに現れた順番に処理されます。 <Directory> (上のグループ 1) はディレクトリが短いものから長いものへと処理されます。ですから、 例えば <Directory /var/web/dir> は <Directory /var/web/dir/subdir> の前に処理されます。複数の <Directory> セクションが 同じディレクトリに 適用される場合は、設定ファイル中の順番に従って処理されます。 Include によって挿入された設定は 挿入しているファイルの Include ディレクティブの位置にあったかのように扱われます。 <VirtualHost> セクション中のセクションは バーチャルホストの定義の外側の対応するセクションの後に適用されます。これによりバーチャルホストが メインのサーバ設定を上書きできるようなります。
https://httpd.apache.org/docs/current/ja/sections.html
<Directory "/foo"> RewriteEngine On RewriteRule testrule4 - [R=405,L] </Directory> <Directory "/foo/bar"> RewriteEngine On RewriteRule testrule4 - [R=406,L] RewriteOptions Inherit </Directory> <VirtualHost *:80> ServerName example.com DocumentRoot /foo RewriteEngine On RewriteRule testrule4 - [R=407,L] </VirtualHost>
/foo → /foo/bar → VirtualHost の順
同じルールの場合は → 407