Apache AliasMatch и перезапись в стиле ZF приводят к внутреннему перенаправлению обратно в корневой каталог - PullRequest
1 голос
/ 02 июля 2019
  • У меня есть этот vhost: www.example.com -> /var/www/html/public
  • Цель состоит в том, чтобы некоторые запросы обрабатывались от /var/www/test/public
  • Каждый public/ dirимеет index.php и .htaccess с перезаписью ZF
  • Директива apache Alias работает, как и ожидалось.
  • «Эквивалентная» директива AliasMatch соответствует, но в итоге обслуживаетсяисходный корень документа.

Вот конфигурация vhost:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com

    DirectoryIndex index.php
    DocumentRoot "/var/www/html/public"
    <Directory /var/www/html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # this AliasMatch does not work (matches but ends up being served by /var/www/html/public) 
    AliasMatch "^/?(test-alias-match)$" "/var/www/test2/public/$1"
    <Directory /var/www/test2/public>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # this Alias works
    Alias /test-alias /var/www/test/public
    <Directory /var/www/test/public>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Logging
    LogLevel debug rewrite:trace8 alias:trace8
</VirtualHost>

Вот .htaccess (без изменений от скелета ZF):

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting or installed the project in a subdirectory,
# the base path will be prepended to allow proper resolution of
# the index.php file; it will work in non-aliased environments
# as well, providing a safe, one-size fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]

Директива Alias работает как положено.Тем не менее, у меня есть несколько Uris, которые я хотел бы указать на альтернативное местоположение, которое может быть легко сопоставлено с регулярным выражением AliasMatch.

Вот журнал из запроса к http://www.example.com/test-alias (РАБОТАЕТ):

app_1          | [Tue Jul 02 14:18:07.350018 2019] [authz_core:debug] [pid 8] mod_authz_core.c(809): [client 172.18.0.1:36844] AH01626: authorization result of Require all granted: granted
app_1          | [Tue Jul 02 14:18:07.360081 2019] [authz_core:debug] [pid 8] mod_authz_core.c(809): [client 172.18.0.1:36844] AH01626: authorization result of <RequireAny>: granted
app_1          | [Tue Jul 02 14:18:07.374895 2019] [authz_core:debug] [pid 8] mod_authz_core.c(809): [client 172.18.0.1:36844] AH01626: authorization result of Require all granted: granted
app_1          | [Tue Jul 02 14:18:07.374958 2019] [authz_core:debug] [pid 8] mod_authz_core.c(809): [client 172.18.0.1:36844] AH01626: authorization result of <RequireAny>: granted
app_1          | [Tue Jul 02 14:18:07.375007 2019] [rewrite:trace3] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a60a0/initial] [perdir /var/www/test/public/] strip per-dir prefix: /var/www/test/public/ -> 
app_1          | [Tue Jul 02 14:18:07.375435 2019] [rewrite:trace3] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a60a0/initial] [perdir /var/www/test/public/] applying pattern '^.*$' to uri ''
app_1          | [Tue Jul 02 14:18:07.375594 2019] [rewrite:trace4] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a60a0/initial] [perdir /var/www/test/public/] RewriteCond: input='/var/www/test/public/' pattern='-s' => not-matched
app_1          | [Tue Jul 02 14:18:07.375617 2019] [rewrite:trace4] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a60a0/initial] [perdir /var/www/test/public/] RewriteCond: input='/var/www/test/public/' pattern='-l' => not-matched
app_1          | [Tue Jul 02 14:18:07.375638 2019] [rewrite:trace4] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a60a0/initial] [perdir /var/www/test/public/] RewriteCond: input='/var/www/test/public/' pattern='-d' => matched
app_1          | [Tue Jul 02 14:18:07.375653 2019] [rewrite:trace1] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a60a0/initial] [perdir /var/www/test/public/] pass through /var/www/test/public/
app_1          | [Tue Jul 02 14:18:07.377819 2019] [authz_core:debug] [pid 8] mod_authz_core.c(809): [client 172.18.0.1:36844] AH01626: authorization result of Require all granted: granted
app_1          | [Tue Jul 02 14:18:07.378056 2019] [authz_core:debug] [pid 8] mod_authz_core.c(809): [client 172.18.0.1:36844] AH01626: authorization result of <RequireAny>: granted
app_1          | [Tue Jul 02 14:18:07.381294 2019] [rewrite:trace3] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a20a0/subreq] [perdir /var/www/test/public/] strip per-dir prefix: /var/www/test/public/index.php -> index.php
app_1          | [Tue Jul 02 14:18:07.381490 2019] [rewrite:trace3] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a20a0/subreq] [perdir /var/www/test/public/] applying pattern '^.*$' to uri 'index.php'
app_1          | [Tue Jul 02 14:18:07.382500 2019] [rewrite:trace4] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a20a0/subreq] [perdir /var/www/test/public/] RewriteCond: input='/var/www/test/public/index.php' pattern='-s' => matched
app_1          | [Tue Jul 02 14:18:07.382736 2019] [rewrite:trace1] [pid 8] mod_rewrite.c(476): [client 172.18.0.1:36844] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a20a0/subreq] [perdir /var/www/test/public/] pass through /var/www/test/public/index.php
app_1          | [Tue Jul 02 14:18:07.689701 2019] [deflate:debug] [pid 8] mod_deflate.c(853): [client 172.18.0.1:36844] AH01384: Zlib: Compressed 113198 to 27448 : URL /test-alias/index.php

Вот журнал из запроса к http://www.example.com/test-alias-match (в конечном итоге обслуживает /var/www/html/public/index.php вместо ожидаемого /var/www/test2/public/index.php):

app_1          | [Tue Jul 02 14:20:02.945574 2019] [authz_core:debug] [pid 12] mod_authz_core.c(809): [client 172.18.0.1:38626] AH01626: authorization result of Require all granted: granted
app_1          | [Tue Jul 02 14:20:02.951123 2019] [authz_core:debug] [pid 12] mod_authz_core.c(809): [client 172.18.0.1:38626] AH01626: authorization result of <RequireAny>: granted
app_1          | [Tue Jul 02 14:20:02.951167 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] strip per-dir prefix: /var/www/test2/public/test-alias-match -> test-alias-match
app_1          | [Tue Jul 02 14:20:02.951184 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] applying pattern '^.*$' to uri 'test-alias-match'
app_1          | [Tue Jul 02 14:20:02.954211 2019] [rewrite:trace4] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] RewriteCond: input='/var/www/test2/public/test-alias-match' pattern='-s' => not-matched
app_1          | [Tue Jul 02 14:20:02.954383 2019] [rewrite:trace4] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] RewriteCond: input='/var/www/test2/public/test-alias-match' pattern='-l' => not-matched
app_1          | [Tue Jul 02 14:20:02.954430 2019] [rewrite:trace4] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] RewriteCond: input='/var/www/test2/public/test-alias-match' pattern='-d' => not-matched
app_1          | [Tue Jul 02 14:20:02.954545 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] strip per-dir prefix: /var/www/test2/public/test-alias-match -> test-alias-match
app_1          | [Tue Jul 02 14:20:02.954561 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] applying pattern '^(.*)' to uri 'test-alias-match'
app_1          | [Tue Jul 02 14:20:02.954702 2019] [rewrite:trace4] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] RewriteCond: input='/test-alias-match::test-alias-match' pattern='^(/.+)/(.*)::\\2$' => not-matched
app_1          | [Tue Jul 02 14:20:02.954718 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] strip per-dir prefix: /var/www/test2/public/test-alias-match -> test-alias-match
app_1          | [Tue Jul 02 14:20:02.954757 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] applying pattern '^(.*)$' to uri 'test-alias-match'
app_1          | [Tue Jul 02 14:20:02.954774 2019] [rewrite:trace2] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] rewrite 'test-alias-match' -> '/index.php'
app_1          | [Tue Jul 02 14:20:02.954790 2019] [rewrite:trace2] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] trying to replace context docroot /var/www/html/public with context prefix 
app_1          | [Tue Jul 02 14:20:02.954804 2019] [rewrite:trace1] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87740a0/initial] [perdir /var/www/test2/public/] internal redirect with /index.php [INTERNAL REDIRECT]
app_1          | [Tue Jul 02 14:20:02.975840 2019] [authz_core:debug] [pid 12] mod_authz_core.c(809): [client 172.18.0.1:38626] AH01626: authorization result of Require all granted: granted
app_1          | [Tue Jul 02 14:20:02.976115 2019] [authz_core:debug] [pid 12] mod_authz_core.c(809): [client 172.18.0.1:38626] AH01626: authorization result of <RequireAny>: granted
app_1          | [Tue Jul 02 14:20:02.976146 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a96c0/initial/redir#1] [perdir /var/www/html/public/] strip per-dir prefix: /var/www/html/public/index.php -> index.php
app_1          | [Tue Jul 02 14:20:02.976165 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a96c0/initial/redir#1] [perdir /var/www/html/public/] applying pattern '.*' to uri 'index.php'
app_1          | [Tue Jul 02 14:20:02.976200 2019] [rewrite:trace5] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a96c0/initial/redir#1] setting env variable 'HTTP_AUTHORIZATION' to ''
app_1          | [Tue Jul 02 14:20:02.976216 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a96c0/initial/redir#1] [perdir /var/www/html/public/] strip per-dir prefix: /var/www/html/public/index.php -> index.php
app_1          | [Tue Jul 02 14:20:02.976260 2019] [rewrite:trace3] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a96c0/initial/redir#1] [perdir /var/www/html/public/] applying pattern '^.*$' to uri 'index.php'
app_1          | [Tue Jul 02 14:20:02.976331 2019] [rewrite:trace4] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a96c0/initial/redir#1] [perdir /var/www/html/public/] RewriteCond: input='/var/www/html/public/index.php' pattern='-s' => matched
app_1          | [Tue Jul 02 14:20:02.976378 2019] [rewrite:trace1] [pid 12] mod_rewrite.c(476): [client 172.18.0.1:38626] 172.18.0.1 - - [www.example.com/sid#7f7cc887ebf8][rid#7f7cc87a96c0/initial/redir#1] [perdir /var/www/html/public/] pass through /var/www/html/public/index.php

1 Ответ

0 голосов
/ 09 июля 2019

Для справки, я решил эту проблему, используя AliasMatch для направления запросов в файл index.php, фактически минуя перезаписи .htaccess. Примерно так:

AliasMatch "^/?(test-alias-match)(.*)$" "/var/www/test2/public/index.php"
...