Stop .htaccess blocking cron jobs

Starkers asked:

I have a php script on my site that, when run, changes the ‘Dog of the Day’ on my home page. I have a cron job set up to run the script once a day, and everything works perfectly.

I’ve secured my ‘scripts’ directory with an .htaccess file, because if someone stumbles upon the php script, it’ll be run, causing the ‘Dog of the Day’ to be updated.

ErrorDocument 404 /404.php

order deny,allow
deny from all
allow from 127.0.0.1

I changed the cron job to run every minute, but the .htaccess is blocking it, despite 127.0.0.1 being whitelisted. I changed 127.0.0.1 to my own ip, and I can access it, so it’s not a problem with syntax.

I’m a graphics designer by trade, and a front-end coder at a push, so keep it simple please!

Thanks a lot!

My answer:


Your script being run from cron is not accessing your own site on the same server via 127.0.0.1, but by its actual IP address.

For instance, you are probably doing something like:

curl http://www.example.com/scripts/update-dog-of-the-day.php

But www.example.com doesn’t resolve to localhost, but to your public IP address.

To resolve the issue, whitelist your public IP address instead.

Allow from 203.0.113.17

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.