Overview of .htaccess

1. What is the .htaccess File?

• A configuration file for Apache servers.

• Allows you to control server behavior on a per-directory basis.

• Commonly used for redirects, URL rewriting, access controls, and more.

2. Default .htaccess in WordPress

• Automatically generated by WordPress to handle permalinks.

• Example of a default .htaccess file:

# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

# END WordPress

• WordPress-specific directives should not be removed unless you’re familiar with the consequences.

Redirect Basics

1. Why Use Redirects?

• Maintain SEO value for old URLs.

• Avoid 404 errors by pointing outdated pages to relevant content.

• Improve user experience by ensuring seamless navigation.

2. Redirect Types

301 Redirect (Permanent): Used when a URL is permanently moved.

302 Redirect (Temporary): Used for temporary changes.

Other Redirect Codes (e.g., 307, 410) for advanced cases.

Examples of Common .htaccess Redirects

1. Redirecting www to Non-www

• Ensures consistency for SEO and avoids duplicate content issues.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]

RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

2. Redirecting Non-www to www

• Same purpose as above but reversed.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

3. Redirecting to HTTPS

• While WordPress handles this well with its site settings, enforcing HTTPS in .htaccess can ensure all traffic is secure.

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

4. Redirecting Old URLs to New URLs

• Perfect for updating pages while keeping link equity intact.

Redirect 301 /old-page https://example.com/new-page

5. Redirecting an Entire Directory

• If you’ve moved or removed a directory, you can redirect it:

RedirectMatch 301 ^/old-directory/(.*)$ https://example.com/new-directory/$1

6. Redirecting Pages with Query Parameters

• For more complex redirects:

RewriteCond %{QUERY_STRING} ^id=123$

RewriteRule ^old-page$ https://example.com/new-page? [L,R=301]

When to Use .htaccess vs. WordPress Plugins

Use .htaccess When:

  • You need server-level control for performance.
  • Your site requires redirects before WordPress loads.

Use Plugins When:

  • You’re not comfortable editing .htaccess.
  • You need a user-friendly interface for managing redirects (e.g., Redirection plugin).

Additional Use Cases for .htaccess

1. Blocking Specific IPs

Deny from 192.168.1.1

2. Custom Error Pages

ErrorDocument 404 /custom-404.html

3. Caching and Performance Optimization

<IfModule mod_expires.c>

ExpiresActive On

ExpiresByType image/jpg "access plus 1 month"

ExpiresByType text/css "access plus 1 week"

</IfModule>

Best Practices for Editing .htaccess

  1. Backup Your .htaccess File

• Always save a copy before making changes.

  1. Test Changes

• Use a staging environment to test before applying to the live site.

  1. Check Permissions

• Ensure the file has proper permissions (typically 644) to avoid errors.

Need help with your .htaccess file or configuring redirects? Contact Zebra Sage for expert guidance to optimize your site’s performance and SEO.

«