Disable Direct IP Access to Apache Website

Apache helicopter.
Image by mrminibike from Pixabay.

I want to share how I solved an issue with Cache Enabler caching duplicate copies of my WordPress website content. I should note that my website is hosted on a shared host that is running Apache. If you are using Cache Enabler, you will see your pages and posts cached under, for example:


$ ls

All the cached pages and posts are under the website domain-name directory. My problem was, I was also seeing two additional directories that looked like:

The IP address was the address being used by my website domain. Under these “IP” directories, I was seeing duplicate copies of my posts and pages being cached. And, when I tried to clear my cache, the files under the IP directories were not being cleared.

My solution was to add a bit of code to my .htaccess file. I had to use the .htaccess method because my site is on a shared Apache host. This is the code that I added:

# public_html/

# BEGIN My additions
<IfModule modrewrite.c>
RewriteEngine On

# If the user/agent is trying to access the domain IP directly ...
RewriteCond %{HTTP_HOST} ^12\.34\.56\.79

# Then deny all access
RewriteRule ^.*$ - [F,L]
# END My additions

A few notes. Of course, use your actual website domain IP address. I placed this chunk of code at the beginning of my .htaccess file in my WordPress site root directory. If you had the same issue as I did with Cache Enabler creating the “IP” directories, you will have to manually remove them. They should not reappear.

The RewriteRule generates a “403 Forbidden” response for any direct IP access attempted.

If this post was useful, please consider buying me a coffee at Ko-Fi. This will help me with my website expenses. Thanks!