Articles

Permanent 301 Redirect .htaccess Code Example

Permanent 301 Redirects

A redirect allows you to send users and search engines to a URL that’s different than what was originally requested. There are a handful of URL redirection types such as a 301 moved permanently, 302 found (moved temporarily), 307 temporary redirect, HTTP header redirect, meta refresh, javascript redirect, and iframe/frameset redirect. In this article, we’re going to cover the permanent 301 redirect.

Looking for a sweet deal? Check out our coupons and Shared hosting, VPS & Dedicated servers today!

What is a Permanent 301 Redirect?

If you’re a webmaster or someone who manages their own sites, a 301 redirect is an essential tool to have in your toolbox. An example of a 301 redirect is when you redirect www.oldurl.com to www.newurl.com or www.yourdomain.com/oldsite to www.yourdomain.com/newsite.

When considering to use a redirect, you’ll want to understand the benefits of using the proper redirect for your SEO (Search Engine Optimization) efforts. A permanent 301 redirect, which refers to the http status code, tells the search engines that the original URL has now moved or updated to a new URL. Using a 301 redirect passes, what many refer to as link juice, to the new URL allowing you to retain your organic rankings for that particular page. The SEs (Search Engines) crawlers will recognize the 301 status code on your page and know to index your site with the new URL.

What is an .htaccess?

An .htaccess file is a configuration file used on web servers running the Apache Web Server software. The .htaccess file is placed in a directory and contains directives that are loaded by the Apache Web Server. In short, these directives allow you to alter the server config files.

The .htaccess can be edited in File Manager using the Code Edit or locally on your computer using a text editor.

You might be wondering why the .htaccess starts with a period. That’s to keep the file hidden in the file directory.

301 Redirect .htaccess Code Example

Below this text are a few examples of .htaccess code for 301 redirects. Please read the descriptions below to refer to the correct redirect code example you’re looking for. We highly recommend that you copy and backup your original .htaccess file before making any changes.

Redirect http to https:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If the above doesn’t work, you’ll want to try using the rule below and replace yourdomain.com with your domain’s URL. Depending on what your server is configured for, you can use port 80 or 443.


RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

Redirect an old page URL to a new page URL:

Redirect 301 /old-page/ http://www.yourdomain.com/new-page/

Redirect root domain to new domain:


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

WordPress redirect old domain to new domain:


RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

WordPress redirect old domain, including posts and pages to new domain:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} olddomain\.com$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

How to Create a 301 Redirect in cPanel

There may be some of you who are looking to create a redirect from an old domain to a new domain, but don’t want to touch your .htaccess file. This is fine! For some people, editing the .htaccess file can be intimidating. Fortunately, you can easily create a 301 redirect inside of cPanel.

Follow these instructions below to create a redirect using cPanel:

Log into your cPanel account. These details were sent over to you in your hosting account Welcome email when you first signed up. Look for the DOMAINS heading and click the Redirects icon.

Next, you’ll Add Redirect on the next page. Here you’ll want to enter the following:

  • Type: Permanent (301)
  • https?://(www.)?: olddomain.com (select your domain in the drop down)
  • /: Keep this blank
  • Redirects to: http://newdomain.com (type the full domain out)

You’ve now successfully added a permanent 301 redirect inside of cPanel.

If you are a customer and are unsure of what to do, please contact support before making any changes to your .htaccess. Modifying an .htaccess file can cause severe consequences such as breaking your entire site.

Looking for reliable Windows or Linux hosting with Award Winning Customer Support?
Grab your hosting today with a 20% discount. All plans come with our 60 Money Back Guarantee!

Previous ArticleNext Article