Peak Positions SEO Logo 231-922-9460

A Simple Way to Stop Malicious Requests on Your WordPress Website

January 9th, 2023

A malicious request on a WordPress website signifies an HTTP request made with harmful intent to exploit vulnerabilities and compromise the site’s security. These requests come in various forms, including SQL injection, Cross-Site Scripting, brute force attacks, and file inclusion attempts, among others. Hackers often target WordPress sites due to their popularity, making it imperative for website owners to prioritize security measures.

Many different WordPress plugins can help prevent malicious requests on your website, however some users may not want to rely on a plugin or want a more custom solution. If you are looking for an easy way to prevent hackers from targeting your WordPress website use this snippet of code in your functions.php file.

How do I edit the functions.php file in my WordPress theme?

Editing the functions.php file in WordPress requires caution and some experience with PHP. To access and modify this file, go to your WordPress dashboard, click on “Appearance,” and select “Theme Editor.” Find the functions.php file on the right-hand side and click on it. You can then add your custom PHP code between the opening tags. It’s essential to be experienced or seek help if you’re not confident in your PHP coding skills, as incorrect code can lead to errors or even break your website. You can also edit this file via a FTP program in which you have access to your servers files. Your hosting provider should provide you FTP access to your website if requested. Always make backups of your website while modifying the functions.php file to ensure you can revert back to a previous version if any issues come up.

This code snippet after being added to your functions.php file will reject malicious URL requests. It first confirms if a user is logged in and not an administrator. Then, it inspects the incoming request for specific keywords or patterns associated with malicious intent, like “eval(” or “UNION+SELECT.” If any of these patterns are found, the code returns a “Request-URI Too Long” status, effectively blocking the request. While this code adds a layer of security, it should be part of a broader security strategy, and experienced users should implement it carefully to avoid disrupting legitimate requests.

global $user_ID; if($user_ID) {
    if(!current_user_can('administrator')) {
        if (strlen($_SERVER['REQUEST_URI']) > 255 ||
            stripos($_SERVER['REQUEST_URI'], "eval(") ||
            stripos($_SERVER['REQUEST_URI'], "CONCAT") ||
            stripos($_SERVER['REQUEST_URI'], "UNION+SELECT") ||
            stripos($_SERVER['REQUEST_URI'], "base64")) {
                @header("HTTP/1.1 414 Request-URI Too Long");
                @header("Status: 414 Request-URI Too Long");
                @header("Connection: Close");
                @exit;
        }
    }
}

Preventing malicious requests is paramount for security and search engine optimization (SEO). For website security, preventing these requests safeguards your website from potential breaches, data loss, and malware distribution, preserving its integrity and user trust. This is also important for SEO, as a secure website is favored by search engines, with Google prioritizing security as a ranking factor. By blocking malicious requests, you mitigate the risk of falling victim to SEO-damaging issues such as content injection or defacement, ensuring your website maintains a positive online reputation and visibility in search results.

TOP 20 SEO COMPANY WORLDWIDE SIX YEARS IN A ROW

Our SEO firm is comprised of a world class team of qualified coders, web designers, developers & SEO copywriters who strive to bring clients the highest quality SEO services in the industry.

Free Website Audit
Top 20 SEO Company Worldwide
Contact Us

Peak Positions SEO
10850 East Traverse Highway, Suite 2290
Traverse City, MI 49684

231-922-9460

info@peakpositions.com