I want to write a plugin to return HTTP 404 when user request license.txt, what is the correct action to hook (both efficient and effective way to block)?
Update:
Because I don't have control to the web server, I must do this as a plugin
Solution is actually pretty straightforward. You need to create plugin which writes to .htaccess.
<?php /* Plugin Name: Licence redirect Description: Redirects license.txt. to 404 Author: J. Wrong Version: 0.1 */ ?> <?php function lr_flush_rewrites() { global $wp_rewrite; $wp_rewrite->flush_rules(); } function lr_add_rewrites() { global $wp_rewrite; $lr_wp_rules = array( 'license\.txt$' => '[R=404,L]', ); $wp_rewrite->non_wp_rules = $lr_wp_rules + $wp_rewrite->non_wp_rules; } register_activation_hook( __FILE__, 'lr_flush_rewrites' ); add_action('generate_rewrite_rules', 'lr_add_rewrites');
You can't. With a standard WordPress .htaccess, requests to static files are not passed to PHP at all, so there is no way to hook them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With