Request to Improve WordPress Plugin for .htaccess Change Detection

Ahmad shared this idea 2 weeks ago
Open Discussion

Dear scalahosting Team,

I hope you're doing well.

I’m currently using a custom WordPress plugin designed to monitor changes in the .htaccess file. The plugin is intended to display a warning in the WordPress admin dashboard whenever the .htaccess file is modified. This is especially important for sites running on LiteSpeed Web Server, where such changes require a manual restart for them to take effect.

What the plugin currently does:
It checks if the .htaccess file has changed (using a hash).

If a change is detected, it shows a warning in the WordPress admin panel.

The admin can dismiss the message manually, or reset the hash through a URL.

Limitations and what needs improvement:
The warning does not disappear automatically.
Once the warning appears, it stays there until dismissed manually — even if the .htaccess change has been handled and the server restarted.
Please improve this behavior so that the warning disappears automatically once the server restart is confirmed (e.g., by writing a flag file or checking a service status).

LiteSpeed Web Server is not restarted automatically.
Currently, the plugin only shows a message — it does not perform the restart of LWS.
Please add a mechanism that will automatically restart the LiteSpeed Web Server when a .htaccess change is detected.

This could be achieved via:

A secure script on the server side (cronjob or background service).

Or by allowing the plugin to send a request/trigger a restart through a controlled interface.

I'm attaching the current full version of the plugin

/*
Plugin Name: HTACCESS Change Warning
Description: Shows an admin notice when the .htaccess file has changed, to remind about restarting LiteSpeed Web Server.
Version: 1.1
Author: Your Name
*/

add_action('admin_notices', function () {
if (!current_user_can('manage_options')) return;

$file = ABSPATH . '.htaccess';
if (!file_exists($file)) return;

$current_hash = md5_file($file);
$stored_hash = get_option('sharpen_last_htaccess_hash');
$dismissed = get_option('sharpen_htaccess_notice_dismissed', false);

if ($stored_hash === false) {
update_option('sharpen_last_htaccess_hash', $current_hash);
return;
}

// If the file has changed - reset the status of the notice
if ($stored_hash !== $current_hash) {
update_option('sharpen_htaccess_notice_dismissed', false);
update_option('sharpen_last_htaccess_hash', $current_hash);
$dismissed = false;
}

if ($stored_hash === $current_hash && $dismissed) {
return;
}

echo '<div class="notice notice-error is-dismissible" style="background-color: #ffe9e0; border-left: 5px solid #cc0000; padding: 12px; margin: 20px 0; position: relative;">
<button type="button" class="notice-dismiss" onclick="location.href=\'' . admin_url('?dismiss_htaccess_notice=1') . '\'" style="text-decoration: none; border: none; background: transparent; cursor: pointer;">
<span class="screen-reader-text">Close</span>
</button>
<p><strong>Important:</strong> The <code>.htaccess</code> file has changed. Please restart the <strong>LiteSpeed Web Server</strong> for the changes to take effect.</p>
<p><a href="https://www.fast-servers.co.il/?p=11022" target="_blank" rel="noopener noreferrer">Click here for restart instructions</a></p>
</div>';
});

// Handling the notice dismissal
add_action('init', function () {
if (isset($_GET['dismiss_htaccess_notice']) && current_user_can('manage_options')) {
update_option('sharpen_htaccess_notice_dismissed', true);
wp_redirect(remove_query_arg('dismiss_htaccess_notice'));
exit;
}

// Manual reset of the hash
if (isset($_GET['sharpen-reset-htaccess-hash']) && current_user_can('manage_options')) {
$file = ABSPATH . '.htaccess';
if (file_exists($file)) {
update_option('sharpen_last_htaccess_hash', md5_file($file));
update_option('sharpen_htaccess_notice_dismissed', false);
wp_die('HTACCESS hash reset. You may now refresh the admin area.');
}
}
});

Please review and enhance it with the two improvements above.

Let me know if you have any questions or need clarification on the logic.

And if you want, you can use this script for all your WordPress websites if it works
Thank you in advance!

Best regards,
Ahmad Mhamed

0 votes

Comments (0)

Leave a Comment