In the default .htaccess rules for a WordPress 4.7 multisite subdomain installation, I'm trying to understand the purpose for one particular rule.
First I'll present the rule on its own
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
In isolation, to me, it would appear that the rule would simply rewrite any url in wp-content or wp-admin or wp-includes to itself. Since it comes after the file system checks, we know it doesn't exist on the file system, so I'm at a loss for what it's trying to do.
Now here's the entirety of the suggested .htaccess file. The rule is third from the end.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
Any insight that anyone can shed would be fantastic. I'm getting an error - "Request exceeded the limit of 10 internal redirects due to probable configuration error." - whenever anyone tries to access any directory beyond wp-content/ that doesn't exist. I would be hoping for a 404 instead.
For what it's worth, this appears to be a long running WP issue per this ticket
https://core.trac.wordpress.org/ticket/20746
There are suggestions of fixes there, but I'd like to understand any change I deploy there since it won't be "official WP". I'm thinking that understanding how that rule could lead to anything other than a self-rewrite would help me be confident in deployment of one of the suggested changes.
EDIT: Here's an example of the rule being applied:
[rid#7f58a5a5dcb0/initial/redir#2] [perdir /var/www/wordpress/] applying pattern '^(wp-(content|admin|includes).*)' to uri 'wp-content/invalid_directory/nothing.png'
[rid#7f58a5a5dcb0/initial/redir#2] [perdir /var/www/wordpress/] rewrite 'wp-content/invalid_directory/nothing.png' -> 'wp-content/invalid_directory/nothing.png'
[rid#7f58a5a5dcb0/initial/redir#2] [perdir /var/www/wordpress/] add per-dir prefix: wp-content/invalid_directory/nothing.png -> /var/www/wordpress/wp-content/invalid_directory/nothing.png
[rid#7f58a5a5dcb0/initial/redir#2] [perdir /var/www/wordpress/] trying to replace prefix /var/www/wordpress/ with /
[rid#7f58a5a5dcb0/initial/redir#2] strip matching prefix: /var/www/wordpress/wp-content/invalid_directory/nothing.png -> wp-content/invalid_directory/nothing.png
RewriteRule ^ (wp- (content | admin | includes). *) $ 1 [L]
Я думаю, что основная цель этой директивы - предотвратить дальнейшую обработку файла (т. Е. Предотвратить пересылку запроса на фронт-контроллер: index.php
) при запросе URL-адреса, который начинается с / wp-content
, / wp-admin
или / wp-includes
.
При перезаписи на себя URL-адрес проходит без изменений и обработка останавливается.
Однако результирующая подстановка также относительна , поэтому все, что указано как RewriteBase
, также будет иметь префикс к перезаписанному URL. В этом случае RewriteBase
просто /
, поэтому он действительно перезаписывается на себя на первом проходе (при условии, что этот файл .htaccess
находится в корне документа) . Я предполагаю, что это должно быть намеренно, иначе они использовали бы дефис ( -
) в качестве замены , как в директиве выше.
Это «не должно» приводить к цикл перезаписи, поскольку обработка должна остановиться, если URL-адрес проходит без изменений (перезапись по существу «игнорируется»).
Однако,чтобы убедиться, что он не изменился, вы можете просто заменить $ 1
на -
в подстановке RewriteRule
.
Если вы включите полную отладку перезаписи, например. Перезапись LogLevel: trace6
в конфигурации сервера на Apache 2.4+ вы должны точно видеть, что происходит, поскольку он должен отображать каждую итерацию перезаписи.