我在Apache / 2.4.34中使用标准的wordpress htaccess规则。如果我添加<If>
语句,则会阻止重写。
# DEBUG is set to TRUE
<If "-T env('DEBUG')">
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]
</If>
除/
外的所有页面均返回:错误的请求
您的浏览器发送了此服务器无法理解的请求。
blog.local 172.20.0.1 - - [30/Sep/2018:17:07:58 +0000] "GET /2018/09/19/popular/ HTTP/1.1" 400 226 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
[Sun Sep 30 17:07:58.202467 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] add path info postfix: /var/www/html/web/2018 -> /var/www/html/web/2018/09/19/popular/
[Sun Sep 30 17:07:58.202511 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] applying pattern '^index\\.php$' to uri '/var/www/html/web/2018/09/19/popular/'
[Sun Sep 30 17:07:58.202529 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] add path info postfix: /var/www/html/web/2018 -> /var/www/html/web/2018/09/19/popular/
[Sun Sep 30 17:07:58.202543 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] applying pattern '^wp-admin$' to uri '/var/www/html/web/2018/09/19/popular/'
[Sun Sep 30 17:07:58.202557 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] add path info postfix: /var/www/html/web/2018 -> /var/www/html/web/2018/09/19/popular/
[Sun Sep 30 17:07:58.202570 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] applying pattern '^' to uri '/var/www/html/web/2018/09/19/popular/'
[Sun Sep 30 17:07:58.202619 2018] [rewrite:trace4] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] RewriteCond: input='/var/www/html/web/2018' pattern='-f' => not-matched
[Sun Sep 30 17:07:58.202638 2018] [rewrite:trace4] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] RewriteCond: input='/var/www/html/web/2018' pattern='-d' => not-matched
[Sun Sep 30 17:07:58.202653 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] add path info postfix: /var/www/html/web/2018 -> /var/www/html/web/2018/09/19/popular/
[Sun Sep 30 17:07:58.202666 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] applying pattern '.' to uri '/var/www/html/web/2018/09/19/popular/'
[Sun Sep 30 17:07:58.202681 2018] [rewrite:trace2] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] rewrite '/var/www/html/web/2018/09/19/popular/' -> 'index.php'
[Sun Sep 30 17:07:58.202693 2018] [rewrite:trace3] [pid 7:tid 140686203255528] mod_rewrite.c(482): [client 172.20.0.1:56856] 172.20.0.1 - - [blog.local/sid#55cd2e1aec50][rid#55cd2e246a80/initial] [perdir *If/] add per-dir prefix: index.php -> *If/index.php
最后一条消息完全是奇怪的:add per-dir prefix: index.php -> *If/index.php
怎么了?
答案 0 :(得分:1)
我在以下htaccess
规则中遇到了类似的情况:
<If "%{REQUEST_URI} =~ m#^/resources/data/character/[0-9]{1,10}/(avatar|refsheet)\.png$#">
# ... Other stuff here ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ resources/generic/default-refsheet.png
</If>
在这种情况下,当最初请求的文件不存在(并且与resources/generic/default-refsheet.png
中的正则表达式匹配时),请求将被重写为 relative 路径<If>
,导致添加了愚蠢的*If/
前缀。
Apache似乎使用了目录前缀*If/
,因为<If>
的内容总是在计算实际目录前缀之前首先应用。
在这种情况下,我找不到使相对的重写URL相对的方法,所以我使用了“绝对”(绝对用引号引起来,因为它实际上仍然相对于虚拟主机的DocumentRoot
,而不是相对的)到htaccess
所在的位置)。因此,将其规则更改为/resources/generic/default-refsheet.png
(首先注意/
):
<If "%{REQUEST_URI} =~ m#^/resources/data/character/[0-9]{1,10}/(avatar|refsheet)\.png$#">
# ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /resources/generic/default-refsheet.png
</If>
在这种情况下,重写的URL不带有哑*If/
目录前缀。
因此,对于您的问题,我会尝试在每条重写的规则前放置/
(或/path/to/wordpress/from/document_root/
):
# DEBUG is set to TRUE
<If "-T env('DEBUG')">
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]
</If>
不过,我不确定它是否会起作用,也不确定为什么会起作用(因此我最初提出的赏金)是100%不确定的。
答案 1 :(得分:0)
您可以将If语句更改为
<If "env('DEBUG') == true">