问题原文
原帖链接 · hendrik-eg · CC BY-SA 4.0
I want these lines from my .htaccess to execute only if the URL is not /wp-admin:
Header add Content-Security-Policy "default-src 'self';"
Header add Content-Security-Policy "script-src 'self';"
My idea was something like this:
<If "%{REQUEST_URI} !^/wp-admin">
</If>
<Else>
Header add Content-Security-Policy "default-src 'self';"
Header add Content-Security-Policy "script-src 'self';"
</Else>
Maybe it would be nice if a logged in user is on the website that the lines also not be executed.
社区高分解答
原帖链接 · MrWhite · CC BY-SA 4.0
Try something like this instead:
<If "%{THE_REQUEST} !~ m#\s/wp-admin#">
Header add Content-Security-Policy "default-src 'self';"
Header add Content-Security-Policy "script-src 'self';"
</If>
This should set the two headers only when the requested URL does not start with /wp-admin.
The check is against THE_REQUEST (as opposed to REQUEST_URI) since REQUEST_URI changes when the URL is rewritten by the WordPress front-controller. THE_REQUEST is the first line of the request headers (a string of the form GET /wp-admin/something HTTP/1.1) and does not change when the request is rewritten.
Maybe it would be nice if a loggedin user is on the website that the lines also not be executed. (I need to do it with htaccess.)
You can't reliably do this with .htaccess. In .htaccess you can only determine whether the authentication cookie is set, not whether it is set correctly.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
