Blog / What is Error 405 (Method Not Allowed) and How to Solve it?

What is Error 405 (Method Not Allowed) and How to Solve it?

by SW Team

The Error 405 “Method Not Allowed ” is an HTTP status code indicating that the server has received a request using an HTTP method that is not allowed for the requested resource. Although the server understands the request, it does not accept the method used, such as GET, POST, PUT, among others.

This error can seriously affect the user experience and the functionality of the website. In this article, we will discuss what the 405 error is, the most common causes of its occurrence, and how to resolve it effectively with practical examples.

Why do I get Error 405 Method Not Allowed?

Error 405 occurs when the server receives an HTTP request with a method that is not allowed for the requested URL.

The most common HTTP methods include:

  • GET: Retrieves a representation of a resource.
  • POST: Sends data for processing to a resource.
  • PUT: Updates a resource with new data.
  • DELETE: Deletes a resource.
  • HEAD: Retrieves the header information of a resource, without the body.
  • OPTIONS: Retrieves the supported methods and other options for a resource.
  • CONNECT: Creates a network connection to a resource.
  • TRACE: Retrieves a diagnostic trace of the actions performed by a resource.
  • PATCH: Applies partial modifications to a resource.

The main causes of this error include:

  • Incorrect HTTP method: A typical example is attempting to make a POST request on a URL that only allows GET.
  • Incorrect server configuration: Failures in server configuration, such as Apache or Nginx, can block certain HTTP methods.
  • API restrictions: Some APIs limit the use of certain methods for certain endpoints.
  • Security rules: Firewalls or security rules on the server may block specific methods.

Variations of Error 405

Depending on the browser or server being used, this error may be presented with different messages, such as:

  • Http Error 405 - Method Not Allowed
  • 405 Not Allowed
  • HTTP 405 Method Not Allowed

How to Fix Error 405 Method Not Allowed?

Below are several strategies that can help you solve this problem, with practical examples that will allow you to understand how to apply the solutions. With SW Hosting, you’ll have peace of mind knowing we have the tools and knowledge to manage and resolve these issues promptly.

1. Verify the correct URL

One of the most common errors that causes a 405 code is using the wrong URL. Make sure the address is correct and matches the page or resource you want to access.

Example. If you try to submit a form via a POST request to www.ejemplo.com/productos/actualizar, but the resource only allows GET, you will get error 405. Verify that the URL you are submitting data to allows POST requests.

Solution:. Check the documentation or server configuration to make sure the URL supports the POST method. If it does not, adjust the URL or method of the request.

2. Check the .htaccess file

If you are using Apache, it is important to check the .htaccess file, as it may contain rules that limit the use of certain HTTP methods. To check it:

  • Locate the .htaccess file in the root directory of your site.
  • Check for directives that limit POST, PUT or other methods.

Example.

<Limit GET POST>
  Order Allow,Deny
  Allow from all
</Limit>

In this case, the .htaccess file is allowing only the GET and POST methods. If you try to use PUT, error 405 will be generated.

Solution: If you need to enable PUT or any other method, adjust the Limit directives:

<Limit GET POST PUT>
  Order Allow,Deny
  Allow from all
</Limit>

3. Configure the Web Server Correctly

The web server, either Apache or Nginx, may be configured to reject certain HTTP methods. Be sure to check the key settings, such as the location blocks in Nginx or the directives in Apache.

Example in Apache:

  1. Option 1: Using <Limit> and <LimitExcept>

If in your Apache configuration you have:

<LimitExcept GET POST>
  Deny from all
</LimitExcept>

This means that any HTTP method other than GET and POST will be blocked, which will cause a 405 error if you try to use DELETE or PUT.

  1. Option 2: Using mod_allowmethods.

Instead of using <Limit> or <LimitExcept> directives, you can use mod_allowmethods to manage the allowed methods in a simpler way. The configuration would look like this:

<Location "/">
   AllowMethods GET POST OPTIONS
</Location>

This explicitly allows only GET, POST and OPTIONS methods, providing a clearer and more secure handling of HTTP requests.

Example in Nginx:

Verify that the limit_except directives in the configuration file are not restricting methods.

location /api/ {
  limit_except GET {
    deny all;
  }
}

If you try to use POST in this block, error 405 will be returned.

Solution: Adjust the location block to allow other methods as needed:

location /api/ {
  limit_except GET POST {
    allow all;
  }
}

4. Disable New Plugins or Extensions

If the 405 error appeared after installing any plugins or extensions in a CMS (such as WordPress), it is advisable to disable them to identify if they are interfering with HTTP requests. Many security plugins may block certain methods by default.

Example. If you installed a security plugin in WordPress that blocks PUT requests to avoid vulnerabilities, it could be generating error 405 in your custom API.

Solution: Temporarily disable the plugin to verify if it is causing the problem. If so, configure your security rules to allow the necessary HTTP methods.

5. Check File Permissions

Make sure files and directories have the correct permissions on your server. Misconfigured permissions can prevent HTTP requests from executing properly.

Example: If the upload.php file does not have the proper permissions (chmod 755), it may prevent POST requests uploading files from completing, which could result in a 405 error.

Solution: Make sure the file permissions are correct by using commands such as chmod or by adjusting the permissions from the server control panel.

6. View Server Logs

The server logs contain detailed information about the errors that occur. Check the logs for more information about error 405 and its possible causes.

  • In Apache, logs are usually located in /var/log/apache2/.
  • In Nginx, the logs are located in /var/log/nginx/.

Example:

tail -f /var/log/apache2/error.log

Check the logs to identify if any specific configuration is causing the error.

7. Make Sure You Are Using the Correct HTTP Method in the API

If the 405 error occurs during interaction with an API, review its documentation to ensure that you are using the correct HTTP method for the endpoint in question.

Example. If the API you are using allows only GET requests to query resources, but you try to use POST, you will receive a 405 error.

Solution: Check the API documentation and adjust the HTTP method according to the correct specifications.

8. Check Firewall Rules

If you have a Web Application Firewall (WAF) or any firewall active on your server, it may be blocking certain HTTP methods.

Example. A WAF could be configured to block methods such as PUT and DELETE for security reasons, generating error 405.

Solution: Adjust the WAF rules to allow the necessary methods or temporarily disable it to verify if the firewall is the cause of the problem.

9. Restore a Backup

If you made recent changes and cannot identify the cause of the error, consider restoring a working backup until you can identify the cause of the problem.

Useful Tools to Identify Incorrect URLs

When a 405 error occurs due to a misconfigured or broken URL, it is advisable to use tools that help identify broken or incorrect links on your website. A recommended tool for this purpose is Dead Link Checker.

This tool will allow you to:

  • Verify broken links on a web site.
  • Identify incorrect URLs that could be generating HTTP errors.
  • Optimize your page performance by correcting links that do not respond correctly.

How to use Dead Link Checker:

  1. Enter your website URL in the verification field.
  2. Click “Check” to start the analysis.
  3. The tool will show you a list of broken links that you can fix or check to avoid errors like 405.

Links to Additional Resources

Conclusion

The Error 405 Method Not Allowed may seem complex, but by applying the described solutions and following the examples, it is possible to identify and correct the cause quickly. Make sure you use the correct HTTP methods and properly configure the server. It will help you prevent future errors and improve the user experience on your website.

In addition, using tools such as Dead Link Checker to check the integrity of the links on your site will help you maintain optimal and error-free performance.

i
Email send icon