Ryan
Ryan

Reputation: 10131

Remove WordPress' license.txt using plugin

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

Upvotes: 2

Views: 2899

Answers (3)

gSaenz
gSaenz

Reputation: 677

There is a plug-in that will do this for you. Just set the file you want to redirect and its target. It even keeps a log

http://wordpress.org/extend/plugins/redirection/

Upvotes: 1

J. Wrong
J. Wrong

Reputation: 1020

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

Upvotes: 7

user149341
user149341

Reputation:

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.

Upvotes: 2

Related Questions