Apache: How to redirect old domain to new domain

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

When you’re running a website, there may come a time when you need to change your domain name. Whether it’s for rebranding purposes, changing service offerings, or otherwise, ensuring that your audience and search engine rankings are preserved during this transition is crucial. That’s where Apache’s redirection capabilities come in handy.

In this tutorial, we’ll explore various methods of redirecting an old domain to a new one using Apache’s .htaccess file. This guidance will suit administrators of any skill level and is relevant as of the last knowledge update in 2023.

Getting Started

Before we delve into redirection, you’ll need to make sure you have access to the .htaccess file on your Apache server. This is a configuration file that allows you to make changes to your website’s behavior at the directory level. If the file doesn’t exist, you can create one in the root folder of your website.

Basic Redirection

A simple domain redirection can be achieved by adding the following lines to your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldexample\.com [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301,NC]

This code tells Apache to turn on the rewrite engine, check if the domain matches ‘oldexample.com’, and if so, rewrite the URL to ‘www.newexample.com’ while preserving the rest of the path after the domain.

The [L,R=301,NC] flags at the end has specific meanings:

  • L – Stops the rewriting process after the preceding directive is processed (Last).
  • R=301 – Sends an HTTP 301 Moved Permanently redirect status. This is crucial for both users and search engines to update their links to the new domain.
  • NC – Makes the rule case-insensitive (NoCase).

Redirecting Specific Pages

Sometimes, you need more granularity in your redirection than a simple domain swap. To redirect specific pages, you can use:

RewriteEngine On
RewriteRule ^oldpage.html$ http://www.newexample.com/newpage.html [R=301,L]

This rule redirects visitors from ‘oldpage.html’ to ‘newpage.html’ at the new domain.

Redirecting with Query Strings

Handling query strings requires a bit more attention. If you want to redirect URLs with query strings, you can add the following:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=10$
RewriteRule ^index\.php$ http://www.newexample.com/newpage.html? [R=301,L]

This checks for a specific query string id=10 and redirects it to ‘newpage.html’—the ‘?’ at the end discards the original query string during the redirect.

Advanced Redirection Techniques

Redirect Based On Request Method

If we want to redirect users based on HTTP request methods (e.g., GET, POST), we can employ:

RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteRule .* http://www.newexample.com%{REQUEST_URI} [R=301,L]

This will apply the 301 redirect only to GET requests.

Wildcard Subdomain Redirection

For those running a multi-subdomain setup, a blanket redirection for all subdomains can be very useful:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.oldexample\.com [NC]
RewriteRule ^ http://www.newexample.com%{REQUEST_URI} [R=301,L]

This rule captures any subdomain of ‘oldexample.com’ and redirects to ‘www.newexample.com’ while preserving the requested path and query.

Preserving HTTPS and Non-WWW Prefixes

SEO best practices recommend keeping the ‘www’ and ‘https’ where applicable. Here’s a rule that accounts for both:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldexample\.com [NC]
RewriteRule ^ https://www.newexample.com%{REQUEST_URI} [R=301,L,NE]

The NE (No Escape) flag prevents the rewrite engine from escaping special characters, and the regex (?:www\.)? matches both ‘www.oldexample.com’ and ‘oldexample.com’.

Conclusion

In this tutorial, we’ve covered a range of methods for redirecting domains in Apache, from basic site-wide redirections to advanced techniques addressing specific needs. Whether you’re dealing with query strings, HTTP methods, or ensuring HTTPS and www prefixes remain intact, Apache’s .htaccess is a powerful tool for managing redirects.

Employing redirects not only provides a seamless transition for users but also ensures search engine rankings transfer to your new domain, preserving your site’s visibility and traffic. With the examples provided, you’re well-equipped to handle most redirection scenarios confidently.