1 Answers
📚 Quick Study Guide: .htaccess for SEO
- 💡 What is .htaccess? It's a directory-level configuration file used by Apache web servers. It allows you to control aspects of your website's behavior on a per-directory basis, without modifying the main server configuration files.
- ⚙️ Key SEO Uses: .htaccess is crucial for implementing 301 (permanent) redirects, enforcing canonical URLs (e.g., www vs. non-www, HTTP vs. HTTPS), creating user-friendly URL rewrites (pretty URLs), blocking malicious bots, improving site speed through caching and GZIP compression, and setting custom error pages.
- ⚠️ Caution: Incorrect `.htaccess` rules can lead to server errors (like 500 Internal Server Error), broken links, or even make your site inaccessible. Always back up your `.htaccess` file before making changes and test thoroughly.
- 📁 Location: Typically placed in the root directory of your website, but can be in any subdirectory to affect only that directory and its subdirectories.
- 📈 Impact on SEO: Proper `.htaccess` configuration improves user experience, enhances search engine crawlability, consolidates link equity (via 301 redirects), and contributes to better site performance, all of which are vital ranking factors.
📝 Practice Quiz: .htaccess for SEO
1. Which .htaccess directive is best for implementing a permanent (301) redirect for an entire domain, ensuring all traffic from olddomain.com goes to newdomain.com for SEO?
A) Redirect 301 / http://www.newdomain.com/
B) RewriteEngine On
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
C) RedirectPermanent / http://www.newdomain.com/
D) RewriteBase /
RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L]
2. To enforce a non-www version of a domain (e.g., example.com instead of www.example.com) using .htaccess for SEO, which RewriteRule block is correct?
A) RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L]
B) RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L]
C) RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L]
D) RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
3. You want to change a dynamic URL like example.com/product.php?id=123 to a clean URL like example.com/product/123/ for better SEO. Assuming the user requests the clean URL, which .htaccess RewriteRule internally processes this request?
A) RewriteRule ^product/([0-9]+)/$ product.php?id=$1 [L]
B) RewriteRule ^product/([0-9]+)/$ product.php?id=$1 [R=301,L]
C) RewriteRule ^product\.php\?id=([0-9]+)$ product/$1/ [L]
D) RewriteRule ^product\.php\?id=([0-9]+)$ /product/$1/ [R=301,L]
4. Which .htaccess directive can be used to block specific user agents (e.g., known spam bots) from accessing your site?
A) Deny from all
Allow from 123.45.67.89
B) BrowserMatchNoCase "BadBot" go_away
Order allow,deny
Deny from env=go_away
C) User-Agent "BadBot" Disallow /
D) RewriteCond %{HTTP_USER_AGENT} "BadBot" [NC]
RewriteRule .* - [F]
5. To enable GZIP compression for common text-based files using .htaccess to improve site speed, which module and directive set are typically used?
A) ExpiresActive On
ExpiresDefault "access plus 1 month"
B) AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript
C) Header set Cache-Control "max-age=3600"
D) FileETag MTime Size
6. To prevent direct access to specific sensitive files like wp-config.php (common in WordPress) via .htaccess, which code snippet is most effective?
A) Order Deny,Allow
Deny from all
B) <Files wp-config.php>
Order allow,deny
Deny from all
</Files>
C) RewriteRule wp-config.php - [F]
D) Disallow: /wp-config.php
7. To set up a custom 404 error page (e.g., error404.html) using .htaccess for a better user experience and SEO, which directive should be used?
A) ErrorDocument 404 /error404.html
B) RewriteRule ^(.*)$ /error404.html [L]
C) ErrorPage 404 /error404.html
D) Redirect 404 /error404.html
Click to see Answers
1. B
2. D
3. A
4. D
5. B
6. B
7. A
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀