I'm trying to add Content Security Policy (CSP) frame ancestors in .htaccess
file to prevent our website from getting iframed on other websites.
Following is the code:
<IfModule mod_headers.c>
Header set Content-Security-Policy "frame-ancestors 'self';"
</IfModule>
This requires mod_headers to be enabled.
But when we enable mod_headers, it gives us Internal Server Error.
Note: We have a plugin installed that uses mod_header in .htaccess. Prevent other sites from showing my site via iframe
Note 2: I am using Apache2 Server
Note 3 (Very Important): When we remove the mod_headers added by the above plugin, the CSP header gets added and no error is encountered. But we do not want to remove the plugin / mod_headers added by the plugin.
Following is the error reported in apache's error logs-
/var/www/html/.htaccess: SetEnvIfNoCaseHeader name regex could not be compiled.
In .htaccess, this is the part where SetEnvIfNoCaseHeader is getting used -
# Force deflate for mangled headers
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
# Don't compress images and other uncompressible content
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png|rar|zip|exe|flv|mov|wma|mp3|avi|swf|mp?g|mp4|webm|webp|pdf)$ no-gzip dont-vary
</IfModule>
</IfModule>
Please Note:
Server version: Apache/2.4.41 (Ubuntu)