Seeing a 406 “Not Acceptable” error appear on your website can be worrying. It means some of your site’s visitors are unable to view pages and content as you intended. But while these errors signal a problem, they don’t have to spell disaster for your site.
With some targeted troubleshooting and tweaks, you can eliminate those 406s and keep your site running smoothly. Here’s what you need to know about fixing 406 errors.
What Causes a 406 Error?
A 406 error occurs when the file format of the requested content doesn’t match the format that the client is able to accept. There are a few common causes:
The Server is Sending the Wrong File Type
For example, the server might be trying to serve a PHP file when the request is expecting HTML. This mismatch in file formats produces the 406 error. Checking server settings can help identify configuration issues causing incorrect content types.
Missing or Incorrect Accept Headers
The client sends Accept headers in the request to indicate the content types it can accept. If these headers are missing or incorrect, the server may try to send incompatible formats. Modifying the headers being sent can resolve these cases.
Problems with Caching Rules
Sometimes caching rules become too restrictive and prevent the server from correctly identifying the acceptable content type for the request. Tweaking caching settings may help serve the right file format.
Issues with Redirects
If improper redirects are in place, they could route requests expecting one content type to pages serving a different, incompatible format. Double-checking redirects can uncover any problematic configurations.
How to Troubleshoot a 406 Error
Diagnosing the source of 406 errors involves some web server and site debugging. Here are some steps to track down the cause:
Check Server Error Logs
The first place to look is your web server’s error log files. The 406 error should be recorded there, along with details like the requested URL, the referring URL, and the headers sent. This information can point to any URL-specific issues.
Examine the Request Accept Headers
Use a tool like curl to mimic the original request and examine the Accept headers. If they are missing or list the wrong content type, that’s likely the problem. You may need to adjust the headers.
Compare the Request and Response Content Types
Check what content types are set on the request versus the actual response. If there’s a mismatch, such as the server trying to serve XML when HTML is expected, that explains the 406.
Test Requests for Affected Pages
Try accessing the URLs producing 406 errors using a variety of clients – browsers, curl, REST tools, etc. If some succeed but others fail, it indicates an incompatibility between that client and the content.
Check for Problems with Caching Rules
Temporarily disable any caching mechanisms to see if that changes the error response. If it does, tweak the caching settings to properly handle the affected requests.
Review Any Redirects Leading to the Pages
Follow any redirects to the 406 error URLs and verify they are appropriate. Errant redirects could be sending the wrong content type for those requests.
How to Fix a 406 Error
Once the source of the problem is identified, you can take steps to resolve the 406 error. Here are some tips:
Update Server Configuration
If the web server is trying to serve PHP files instead of HTML, change its settings to send the proper content types. Restart the server to load the new configuration.
Add or Modify Accept Headers
If the headers are missing or wrong, add or update the Accept header in the requesting application’s code to match the content type served.
Modify Caching Rules
Adjust any caching policies or headers that are too restrictive. Allow the server to correctly identify the request format and serve the right content type.
Fix Faulty Redirects
Point any improper redirects to URLs serving content compatible with the requests. Avoid chained redirects that pass through incompatible file types.
Change the Response Format
As a last resort, modify the HTML, PHP, etc to change the response format on the server side to match what clients expect.
How to Prevent 406 Errors
To avoid future 406 errors, build in robust Accept header handling and implement request-based caching rules:
Include a Default Accept Header
Set a default Accept header in your code and APIs to specify expected content types. This header gets sent when custom ones are not defined.
Check Accept Headers in Code
Before serving a response, validate the Accept header and check if you can send the desired content type. Return a 406 if not.
Base Caching on Request Headers
Cache and serve content based on request Accept headers and content types versus just URL. This allows efficient caching while still serving proper formats.
Monitor for 406 Errors
Track 406 errors in server logs to catch any new cases quickly before they impact users. Set up real-time alerts for these errors.
Conclusion
Equipped with this knowledge of how to investigate and address 406 errors, you can act quickly when these “not acceptable” warnings pop up. Instead of being derailed by incompatible formats, you’ll have a plan to put your site back on track and prevent future conflicts.
Your site’s visitors will appreciate the fast resolution and being able to easily access content again. So don’t let 406 errors keep your site down – leverage them to make your system even more robust.