Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove WordPress' license.txt using plugin

Tags:

php

wordpress

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

like image 827
Ryan Avatar asked Feb 20 '26 03:02

Ryan


2 Answers

Solution is actually pretty straightforward. You need to create plugin which writes to .htaccess.

  1. In the /wp-content/plugins create licence_redirect folder.
  2. In that folder create licence_redirect.php file.
  3. Paste code below to this licence_redirect.php php file:
<?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');
  1. Install and activate plugin.
  2. In admin panel go to Setting -> Permalinks
  3. Press save changes From now on your license.txt requests will be redirected to 404. If you can't create folders on server then you'll need to zip the plugin's folder and upload it using WP admin. Cheers... counting bounty now :P
like image 102
J. Wrong Avatar answered Feb 21 '26 15:02

J. Wrong


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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!