An .htaccess
file is used to configure settings for Apache web servers. It allows you to control various aspects of the server, including URL redirection, security settings, and caching policies. Here are some examples and explanations of common configurations that might be used for a blog:
Basic Structure
Enable Rewrite Engine
apacheRewriteEngine On
Redirect HTTP to HTTPS
Ensures all traffic is encrypted.
apacheRewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Remove WWW
Redirects all traffic from
www.example.com
toexample.com
.apacheRewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Pretty Permalinks
Useful for making blog URLs clean and SEO-friendly.
apacheRewriteRule ^article/([0-9]+)/([a-zA-Z0-9-]+)/?$ /article.php?id=$1&title=$2 [L,QSA]
Block Access to Sensitive Files
Prevents access to
.htaccess
,.htpasswd
, and other sensitive files.apache<Files .htaccess> Order allow,deny Deny from all </Files>
Set Custom Error Pages
Provides custom error pages for a better user experience.
apacheErrorDocument 404 /404.html ErrorDocument 403 /403.html
Example .htaccess File for a Blog
apache# Enable Rewrite Engine RewriteEngine On # Redirect HTTP to HTTPS RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Remove WWW RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] # Pretty Permalinks RewriteRule ^article/([0-9]+)/([a-zA-Z0-9-]+)/?$ /article.php?id=$1&title=$2 [L,QSA] # Block Access to Sensitive Files <Files .htaccess> Order allow,deny Deny from all </Files> # Set Custom Error Pages ErrorDocument 404 /404.html ErrorDocument 403 /403.html # Cache Control <IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpg "access plus 1 year" ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/gif "access plus 1 year" ExpiresByType image/png "access plus 1 year" ExpiresByType text/css "access plus 1 month" ExpiresByType application/pdf "access plus 1 month" ExpiresByType text/x-javascript "access plus 1 month" ExpiresByType application/x-shockwave-flash "access plus 1 month" ExpiresByType image/x-icon "access plus 1 year" ExpiresDefault "access plus 2 days" </IfModule> # Compression <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/json </IfModule> # Prevent Directory Browsing Options -Indexes
Explanation of Additional Features
- Cache Control: Sets expiration headers for different file types, improving load times for returning visitors.
- Compression: Enables Gzip compression for text-based files, reducing the amount of data transferred.
- Prevent Directory Browsing: Disables directory listings to improve security.
By customizing your .htaccess
file, you can enhance your blog's performance, security, and SEO. Always test your .htaccess
file after making changes to ensure your site functions correctly.
Leave a Comment