1. Home
  2. Resources Archive
  3. Picking the Correct .htaccess Scenario

Picking the Correct .htaccess Scenario

It may be easier than you think.

Start by looking at the URL structure of the live site while the future site is under production. This gives you the left side of the 301 equation.

Then look at the URL structure of the site under production. This gives you the right side of the equation.

Once you have that, the rest is easy.

The only configuration directive that changes above the “# 301 specific URL redirects” line in a standard .htaccess file is the Rewrite Engine.

Let’s Explore Some Scenarios

Scenario 1: Same Domain Redirects (non-www to www)

If we are going from https://example.com (left side) to https://www.example.com (right side), use the non-www to www Rewrite Engine for Same Domain.

Configuration:

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

301 Redirects:

For all live site pages, the 301s will be written as shown in the example below:

Redirect 301 /contact https://www.example.com/contact.html

Scenario 2: New Domain Redirects (non-www to www)

If we are going from https://example.com (left side) to https://www.newexample.com (right side), use the non-www to www Rewrite Engine for New Domain.

Configuration:

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

301 Redirects:

For all live site pages, the 301s will be written as shown in the example below:

Redirect 301 /contact https://www.newexample.com/contact.html

Scenario 3: New Domain Redirects (www to www)

If we are going from https://www.oldexample.com (left side) to https://www.newexample.com (right side), use the www to www Rewrite Engine for New Domain.

Configuration:

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

301 Redirects:

For all live site pages, the 301s will be written as shown in the example below:

Redirect 301 /contact https://www.newexample.com/contact.html

Putting It All Together

Here’s how the full .htaccess file might look for each scenario:

Example 1: Same Domain (non-www to www)

# Enable Rewrite Engine
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# 301 specific URL redirects
Redirect 301 /contact https://www.example.com/contact.html
Redirect 301 /about https://www.example.com/about.html
Redirect 301 /old-blog https://www.example.com/new-blog.html

Example 2: New Domain (non-www to www)

# Enable Rewrite Engine
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.newexample.com/$1 [L,R=301]

# 301 specific URL redirects
Redirect 301 /contact https://www.newexample.com/contact.html
Redirect 301 /about https://www.newexample.com/about.html
Redirect 301 /old-blog https://www.newexample.com/new-blog.html

Example 3: New Domain (www to www)

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

# 301 specific URL redirects
Redirect 301 /contact https://www.newexample.com/contact.html
Redirect 301 /about https://www.newexample.com/about.html
Redirect 301 /old-blog https://www.newexample.com/new-blog.html

Summary

Determine Your Current and Future URL Structures: Identify the URLs of your live site and the future site under production.

Choose the Appropriate Rewrite Engine: Based on whether you’re redirecting within the same domain or to a new domain.

Write the 301 Redirects: For each page, create a 301 redirect that points from the old URL to the new URL.

By following these steps, you can easily set up your .htaccess file for the correct scenario, ensuring smooth redirection and preserving your SEO rankings.

Updated on August 27, 2024
Was this article helpful?

Related Articles