Use 301 redirects in your .htaccess file to redirect all URLs from olddomain.com to newdomain.com while preserving the complete URL path structure.
What is .htaccess?
The .htaccess file is a simple text file that controls your website. It tells your web server how to handle visitors and page requests. You can use it to set up redirects without touching complex server settings.
Before You Start – Safety Checklist
- Backup your website files and database completely
- Verify your new domain works and has SSL certificate installed
- Test redirects on staging environment first if possible
- Document your current URL structure for reference
- Choose low-traffic time for implementation (avoid peak hours)
The Complete Solution
Apache .htaccess Method (Most Common)
Step 1: Access your website’s root directory and create/edit the .htaccess file
Step 2: Add this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]
Result: All URLs redirect with preserved paths:
- olddomain.com/page101 → newdomain.com/page101
- olddomain.com/blog/post → newdomain.com/blog/post
- olddomain.com/category/wordpress→ newdomain.com/category/wordpress
Nginx Method
server {
server_name olddomain.com www.olddomain.com;
return 301 https://newdomain.com$request_uri;
}
WordPress Method
Add to functions.php:
function redirect_entire_site() {
if (!is_admin()) {
wp_redirect('https://newdomain.com' . $_SERVER['REQUEST_URI'], 301);
exit();
}
}
add_action('template_redirect', 'redirect_entire_site');
New to WordPress? Check out our guides on what is WordPress and how to build a website to get started.
Note: Learn more about WordPress site migration best practices for complete domain moves.
What This Code Does
- RewriteEngine On: Activates URL rewriting
- RewriteCond: Matches both www and non-www versions
- ^(.*)$: Captures the entire URL path after the domain
- $1: Preserves the captured path in the redirect
- [R=301,L]: Creates permanent redirect and stops processing
Testing Your Redirect
Manual Test:
- Visit olddomain.com/any-page
- Should automatically redirect to newdomain.com/any-page
- Check browser address bar shows new domain
Command Line Test:
curl -I olddomain.com/why-wordpress
# Should return: HTTP/1.1 301 Moved Permanently
# Location: https://newdomain.com/why-wordpress
Why Use 301 Redirects
- Permanent redirect tells search engines about your domain migration
- Keeps SEO rankings with 90-99% link authority transfer (Google says so)
- Saves user bookmarks and stops broken link errors
- High success rate: 95% of good 301 redirects keep search rankings
- Passes link authority: Studies show 301 redirects pass almost all PageRank value
- Prevents traffic loss: Sites lose 35% traffic without redirects during migration
Learn more about 301 vs 302 redirects differences and when to use each type.
Common Issues and Solutions
Problem: Redirect loop error
Solution: Ensure new domain doesn’t redirect back to old domain
Problem: HTTPS not working
Solution: Install SSL certificate on new domain before redirecting
Problem: Subdomains not redirecting
Solution: Add separate rules for each subdomain:
RewriteCond %{HTTP_HOST} ^blog\.olddomain\.com$
RewriteRule ^(.*)$ https://newdomain.com/blog/$1 [R=301,L]
For more troubleshooting help, see our common .htaccess redirect errors guide.
Alternative Methods
DNS-Level Redirect (Cloudflare):
- Pattern:
olddomain.com/*
- Setting: Forwarding URL (301)
- Destination:
https://newdomain.com/$1
Domain Registrar Forwarding:
- Available in most registrar control panels
- Less reliable than server-level redirects
- May not preserve URL paths properly
Key Requirements
- Use 301 redirects (not 302) for permanent migration
- Preserve URL structure with $1 parameter
- Include both www and non-www versions
- Test all major pages after implementation
- Monitor for redirect loops and errors
Pro tip: After setting up redirects, update your Google Search Console and submit a change of address request.
Implementation Time: 5-10 minutes
SEO Effect Timeline: 4-6 weeks for full ranking transfer
How Long Do Redirects Take to Work?
Immediate Effects:
- Visitors instantly redirected to new domain
- Browser bookmarks automatically follow redirects
Search Engine Timeline:
- 1-7 days: Google discovers redirect signals during site crawling
- 2-4 weeks: Search rankings begin transferring to new domain
- 4-8 weeks: Complete SEO authority migration and URL forwarding established
- 3-6 months: Old domain completely removed from search results
Domain migration success: 85% of websites see full ranking recovery within 6 weeks when using proper 301 redirects for site transfer.
Setting up redirects with .htaccess helps visitors reach your new site without broken links. Always test your redirects before going live to avoid loops or errors.