Fun with mod_security, htaccess, and PHP updates

It all started with an updated PHP version. This website had been running on PHP 7.4, which went end of life a year ago. I had requested several times that my hosting provider update to a supported version of PHP for security reasons, but was rebuffed. A week ago I decided to ask yet again, and this time HostGator migrated my site to a server with PHP 8, probably to get me to stop complaining. Post-migration, I changed my site to use PHP 8.1 because trying 8.2 broke everything, clicked around some to make sure everything still appeared to be working, and moved on from what I thought was a successful upgrade.

Problems Found

Today, I get an email from Google Search Console stating that some of my pages were returning 4XX errors. I am certainly not an expert on Google Search Console, but if you have a website, it’s a very good idea to use it to find these kinds of errors. When I looked at the pages that were being flagged as unreachable, I noticed that they were photos from the photo side of my site, which runs on ZenPhoto. All of the affected URLs ended in “jpg.php”, and all of them had a 406 error with the message “Not Acceptable!
An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.”

No, YOU’RE not acceptable!

It appeared that all of the individual photo pages in my albums had this problem, and that meant that phots were pretty much inaccessible. Not good! I did some searching and came across this forum post, which suggested changing the URL options mod_rewrite suffix under “Options…General” to .html from .php:

URL Options in ZenPhoto

Changing this option did solve the 406 Not Acceptable error, and my images were being served up again, with a .jpg.html URL instead of .jpg.php. However, as that forum post stated, changing this option will change your URLs and can mess with SEO. That’s exactly how I became aware of the problem in the first place: the URLs that Google indexed were using the old .jpg.php URLs, and although this website is certainly not popular enough to be getting a lot of traffic to random photos from Google, the fact that this would break URLs in Google search results just annoyed me.

301 To The Rescue

The correct way to deal with this kind of issue is to set up a 301 Moved Permanently redirect on the old URLs to point to the new ones. This is something you can do by using rewrite rules in your .htaccess file. But first, I had to fix the 406 error that caused the issue in the first place.

HostGator controls the mod_security rules, but per this article, you can reach out to them to selectively disable rules if they are causing any problems with your website. So I did this, and they very quickly removed the rule that was causing that error. Once that error was gone, I could add this very simple rewrite rule to rewrite anything that ended with “.jpg.php” to “.jpg.html”:

  RewriteRule ^(.*)\.jpg\.php    $1.jpg.html [R=301,L]

With this rule in place in the .htaccess file in my photo folder, all of my old URLs would redirect to the new one and display properly, something I validated by trying one of the URLs that Google had alerted me on.

I then regenerated my sitemap files so that going forward Google will get the correct URLs for all content, and will monitor those URLs going forward. I also triggered a recheck of the URLs that were throwing errors so Google can validate that the redirect works. And that was that!

Do It The Harder Way

Now, if you were paying attention, at this point you would likely ask “Couldn’t you have skipped most of this by asking HostGator to just fix the mod_security rule and keep the URLs ending in php?” And yes, that would have solved the immediate problem of broken URLs. However, .html is the preferred value for that option now, and it’s one I also prefer. Plus, had I stopped there, I would never have learned how to create those rewrite rules, which is a handy skill to have.

It took a bit more work, but I did learn a few things about .htaccess files and mod_security along the way. And sometimes, you just need to bite the bullet on breaking changes!

Leave a comment

Your email address will not be published. Required fields are marked *