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.
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."
# Return 410 for a specific deleted page
Redirect 410 /old-blog-post.html
# Mark entire blog directory as gone
RedirectMatch 410 ^/blog/
# All HTML files in a directory
RedirectMatch 410 ^/deleted-section/.*\.html$
Use 403 to actively block access to files or directories.
# Deny access to private directory
<Directory /path/to/private>
Require all denied
</Directory>
# Protect configuration files
<FilesMatch "^(config|settings)\.php$">
Require all denied
</FilesMatch>
# Block access to all .txt files
<FilesMatch "\.txt$">
Require all denied
</FilesMatch>
For more complex conditions, use mod_rewrite:
RewriteEngine On
# Return 410 for old URLs with specific parameter
RewriteCond %{QUERY_STRING} ^old=true$
RewriteRule ^.*$ - [G]
# The [G] flag means "Gone" (410 status)
RewriteEngine On
# Block access based on user agent
RewriteCond %{HTTP_USER_AGENT} badbot [NC]
RewriteRule ^.*$ - [F]
# The [F] flag means "Forbidden" (403 status)
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
# Blog was deleted, tell search engines it's gone
RedirectMatch 410 ^/blog/
ErrorDocument 410 "This blog section has been permanently removed."
# 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>
# Old product pages no longer exist
RedirectMatch 410 ^/products/discontinued/
ErrorDocument 410 /errors/product-discontinued.html
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
| 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) | - |
Use 410 when:
Use 301 redirect instead when:
Please donate if helpful