.htaccess Redirects Guide 2026: Complete Setup Tutorial
Arnaud Fosse
Website redirects are essential for maintaining SEO rankings, improving user experience, and managing website migrations. The .htaccess file provides a powerful way to implement various types of redirects on Apache web servers. In this comprehensive guide, we'll explore everything you need to know about creating effective .htaccess redirects in 2026.
Understanding .htaccess and Its Role in Web Development
The .htaccess file is a configuration file used by Apache web servers to control directory-level configurations. When it comes to redirects, .htaccess offers flexibility and immediate implementation without requiring server restarts. This makes it an ideal solution for SEO professionals, web developers, and website administrators.
Located in your website's root directory, the .htaccess file can handle various redirect scenarios, from simple page moves to complex URL restructuring. Understanding how to properly configure these redirects is crucial for maintaining search engine rankings and providing seamless user experiences.
Types of HTTP Redirects and When to Use Them
301 Redirects (Permanent)
301 redirects indicate that a page has permanently moved to a new location. Search engines transfer approximately 90-99% of the original page's ranking power to the new URL. Use 301 redirects when:
- Permanently moving content to a new URL
- Consolidating duplicate pages
- Migrating from HTTP to HTTPS
- Changing domain names
Example syntax:
Redirect 301 /old-page.html /new-page.html302 Redirects (Temporary)
302 redirects indicate temporary moves and don't pass ranking power to the new URL. Use 302 redirects for:
- Temporary maintenance pages
- A/B testing scenarios
- Seasonal content redirects
- Temporary promotional pages
Example syntax:
Redirect 302 /temporary-page.html /current-page.htmlCreating Basic .htaccess Redirects
Single Page Redirects
The simplest redirect moves one specific page to another location:
RewriteEngine On
Redirect 301 /old-product.html /new-product.htmlDirectory Redirects
Redirect entire directories to new locations:
RewriteEngine On
RedirectMatch 301 ^/old-directory/(.*) /new-directory/$1Domain Redirects
Redirect an entire domain to a new domain while preserving URL structure:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$
RewriteRule (.*)$ https://new-domain.com/$1 [R=301,L]Advanced Redirect Techniques
Wildcard and Pattern Matching
Use regular expressions for complex redirect patterns:
RewriteEngine On
# Redirect all product pages with old structure
RewriteRule ^products/([0-9]+)/(.*)$ /shop/item/$1/$2 [R=301,L]
# Redirect old blog structure
RewriteRule ^blog/([0-9]{4})/([0-9]{2})/(.*)$ /articles/$1-$2/$3 [R=301,L]Query Parameter Handling
Redirect URLs with specific query parameters:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^product$ /products/%1? [R=301,L]Mobile Device Redirects
Redirect mobile users to mobile-specific versions:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "android|iphone|ipod|iemobile" [NC]
RewriteRule ^(.*)$ https://m.example.com/$1 [R=302,L]Common Redirect Scenarios and Solutions
HTTP to HTTPS Migration
Force all traffic to use HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]WWW to Non-WWW (or vice versa)
Standardize your domain format for SEO:
# Remove www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Add www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]Trailing Slash Normalization
Ensure consistent URL structure:
RewriteEngine On
# Add trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [R=301,L]Best Practices for .htaccess Redirects
Performance Optimization
Keep your .htaccess file efficient to minimize server load:
- Place most common redirects at the top
- Use specific patterns rather than broad wildcards
- Combine similar rules when possible
- Test redirect chains and avoid multiple hops
SEO Considerations
Properly implemented redirects preserve search engine rankings. Tools like SiteRadar can help monitor redirect implementation and identify potential SEO issues:
- Use 301 redirects for permanent moves
- Avoid redirect chains longer than 3 hops
- Update internal links to point directly to new URLs
- Monitor crawl errors in search console
Testing and Validation
Always test your redirects before deploying:
- Use HTTP status checkers to verify redirect codes
- Test with various browsers and devices
- Check for redirect loops
- Validate that redirects preserve query parameters when needed
Common Mistakes to Avoid
Redirect Loops
Avoid creating infinite redirect loops:
# WRONG - Creates a loop
RewriteRule ^old-page$ /new-page [R=301,L]
RewriteRule ^new-page$ /old-page [R=301,L]Missing L Flag
Always use the L (Last) flag to prevent rule conflicts:
# CORRECT
RewriteRule ^old-page$ /new-page [R=301,L]Incorrect Syntax
Common syntax errors include:
- Missing RewriteEngine On directive
- Incorrect regular expression patterns
- Missing escape characters for special characters
- Wrong redirect status codes
Monitoring and Maintaining Redirects
Regular monitoring ensures your redirects continue working effectively:
- Check server logs for 404 errors
- Monitor page load speeds
- Use website audit tools to identify broken redirects
- Review and clean up outdated redirects periodically
Professional website audit tools can automate much of this monitoring process, providing insights into redirect performance and potential issues.
What is the difference between 301 and 302 redirects?
301 redirects indicate permanent moves and pass 90-99% of SEO ranking power to the new URL, while 302 redirects are temporary and don't transfer ranking power. Use 301 for permanent content moves and 302 for temporary situations like maintenance pages or A/B testing.
How do I redirect an entire domain using .htaccess?
To redirect an entire domain while preserving URL structure, use: RewriteEngine On, RewriteCond %{HTTP_HOST} ^old-domain\.com$ [OR], RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$, RewriteRule (.*)$ https://new-domain.com/$1 [R=301,L]. This maintains the path structure while changing the domain.
What happens if I create a redirect loop?
Redirect loops occur when pages redirect to each other infinitely, causing browsers to display "too many redirects" errors after 10-20 iterations. This breaks user experience and prevents search engines from crawling your content. Always test redirects and use tools to identify loops before deployment.
Can .htaccess redirects slow down my website?
Yes, poorly optimized .htaccess files with complex regex patterns or numerous rules can slow down response times by 50-200ms per request. Minimize this impact by placing common redirects first, using specific patterns, and avoiding unnecessary wildcards. Monitor your site's performance regularly.
How do I test if my .htaccess redirects are working correctly?
Test redirects using HTTP status checkers, browser developer tools, or curl commands to verify status codes (301/302). Check that redirects preserve query parameters when needed, don't create loops, and point to the correct destinations. Online redirect checker tools can automate this testing process.
Conclusion
Mastering .htaccess redirects is essential for maintaining website performance, SEO rankings, and user experience in 2026. By understanding the different redirect types, implementing best practices, and avoiding common pitfalls, you can ensure smooth website migrations and optimal search engine visibility.
Remember to test thoroughly, monitor regularly, and keep your redirect rules organized and efficient. With proper implementation, .htaccess redirects become a powerful tool for website management and SEO optimization.
Discover SiteRadar
Analyze your website for free with our SEO, performance and security audit tool.
View pricing →