Pacific Blue Software Logo

How to Generate HTTP Errors with htaccess

How to return HTTP error codes with htaccess?

Sometimes you need to tell search engines or users that a page is gone, forbidden, or unavailable. You can do this with HTTP status codes in your .htaccess file. Here's how.

The 410 Gone Status

Use 410 when you've permanently deleted content and want search engines to stop indexing it. This is better for SEO than 404 because it tells search engines "don't come back looking for this."

Single URL

# Return 410 for a specific deleted page
Redirect 410 /old-blog-post.html

Entire Directory

# Mark entire blog directory as gone
RedirectMatch 410 ^/blog/

Multiple Pages with Pattern

# All HTML files in a directory
RedirectMatch 410 ^/deleted-section/.*\.html$

The 403 Forbidden Status

Use 403 to actively block access to files or directories.

Block a Directory

# Deny access to private directory
<Directory /path/to/private>
    Require all denied
</Directory>

Block Specific Files

# Protect configuration files
<FilesMatch "^(config|settings)\.php$">
    Require all denied
</FilesMatch>

Block File Types

# Block access to all .txt files
<FilesMatch "\.txt$">
    Require all denied
</FilesMatch>

Using RewriteRule for Errors

For more complex conditions, use mod_rewrite:

410 with RewriteRule

RewriteEngine On

# Return 410 for old URLs with specific parameter
RewriteCond %{QUERY_STRING} ^old=true$
RewriteRule ^.*$ - [G]

# The [G] flag means "Gone" (410 status)

403 with RewriteRule

RewriteEngine On

# Block access based on user agent
RewriteCond %{HTTP_USER_AGENT} badbot [NC]
RewriteRule ^.*$ - [F]

# The [F] flag means "Forbidden" (403 status)

Combining with Custom Error Pages

You can show custom pages for these errors:

# Define custom error pages
ErrorDocument 403 /errors/forbidden.html
ErrorDocument 410 /errors/gone.html

# Then return the appropriate error
Redirect 410 /deleted-page.html

Common Patterns

Deleted Blog Section

# Blog was deleted, tell search engines it's gone
RedirectMatch 410 ^/blog/
ErrorDocument 410 "This blog section has been permanently removed."

Protect Admin Area

# Block public access to admin
<Directory /var/www/html/admin>
    Require ip 192.168.1.0/24
    Require ip 10.0.0.0/8
</Directory>

Remove Old Product Pages

# Old product pages no longer exist
RedirectMatch 410 ^/products/discontinued/
ErrorDocument 410 /errors/product-discontinued.html

Testing Your Rules

Use curl to test the status codes:

# Check if 410 is returned
curl -I https://yoursite.com/deleted-page.html

# Should show:
# HTTP/1.1 410 Gone

# Check if 403 is returned
curl -I https://yoursite.com/private/file.txt

# Should show:
# HTTP/1.1 403 Forbidden

Quick Reference

Code Meaning When to Use htaccess
403 Forbidden Block access to protected content Require all denied or [F]
404 Not Found File doesn't exist (automatic) -
410 Gone Permanently deleted content Redirect 410 or [G]
500 Server Error Application error (not htaccess) -

Common Mistakes to Avoid

SEO Considerations

Use 410 when:

Use 301 redirect instead when:

Useful References


Returning Custom HTTP Error Codes with Apache htaccess


Back to Articles for Developers
Back to Website Articles
How to Hide Download Links with PHP
How to Have Custom Error Pages on XAMPP / Apache

If you found this useful, then please consider making a donation.

paypal
QR Code for donation Please donate if helpful