HTACCESS: Like for Like URL Redirect

When a site is moved to a new location it is important to 301 redirect anyone who happens upon the old pages to the new pages. This helps both visitors and search engines.

Redirection works best when visitors are sent from old URLs to new URLs in one-to-one fashion. For example, the like for like detour from example.com/page-one to example-two.com/page-one.

There’s no need to use scripts to map sites and make millions of “Redirect 301” directives for htaccess files. Provided there is no change in page directory structure we can use a few simple lines to direct traffic from one domain to another domain.

Redirect from old domain to new domain

  1. Create an .htaccess file in the root directory of the site’s old location
  2. Add this snippet to the top of the .htaccess file
    # Set the domain the site was stored at
    RewriteCond %{HTTP_HOST} ^(www\.example\.com)$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
    # Set the domain the site has moved to
    RewriteRule (.*) http://example-two\.com/$1 [R=301,L]
  3. Change example.com and example-two.com to reflect the domain names involved.

Redirect from domain to subdomain

Use this snippet instead

# Set the domain the site was stored at
RewriteCond %{HTTP_HOST} ^(www\.example\.com)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
# Set the domain the site has moved to
RewriteRule (.*) http://sub-domain\.example\.com/$1 [R=301,L]

Redirect from domain to subdirectory

# Set the domain the site was stored at
RewriteCond %{HTTP_HOST} ^(www\.example\.com)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
# Set the directory the site has moved to
RewriteRule (.*) http://example\.com/sub-directory/$1 [R=301,L]

Sharing is caring!

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x