How To Do a 301 Redirect On WordPress Without a Plugin

As a webmaster, there will come a time where you will need to do a 301 redirect for whatever reason. If your website is powered by WordPress you will have the choice of either using a plugin or not using one to perform a 301 redirect.  

A 301 redirection can be performed on a WordPress website without using a plugin by adding a few lines of redirection code to either the .htaccess file or to the functions.php file of the active WordPress theme.  

Performing a 301 redirection without using a plugin requires you to have some knowledge of coding and is more difficult than using one of the popular redirection plugins available on WordPress.

So you may be wondering why some webmasters will choose to do a 301 redirection without using a plugin? 

Well, one reason is that plugins add more overhead to your website which in turn slows it down. Another reason is that plugins can add security vulnerabilities to your website especially if they are not well maintained and updated regularly.

So by minimizing the number of plugins that you use on your website, you will not only reduce the risk of having a slower website, but you will also lower the chance of a security breach on your website.

Throughout the remainder of this tutorial, I will provide an in-depth guide to performing a 301 redirection using your .htaccess and functions.php files. So if you think you got what it takes then get ready and follow along.

301 Redirect WordPress Without Plugin

Table of Contents

Create a 301 Redirect VIA The .htaccess File

The format for a 301 redirect using the .htaccess file goes as follows;

Redirect [status] [URL-path] URL

[status]: This is the type of redirect that you would like to perform. The most commonly performed redirects are 301 and 302. (If you don’t provide the [status] it will automatically default to a 302 redirect).

[URL-path]: This is the URL slug of the page/post that you would like to redirect. (If you fail to provide the [URL-path] the redirection will fail).

URL: This is the page or post that you would like to redirect the user to. (If you fail to provide the URL the redirection will also fail).

Also, there are three types of 301 redirects that you perform using the .htaccess file.

“For the following examples, the URL slug old-post needs to be replaced with the slug of the post that you want to redirect. The URL slug new-post needs to be replaced with the slug of the post that you would like to send the user to.”

A redirect from a page/post to a new page/post on the same website. 

  • Redirect 301 /old-post /new-post

For example, if your old post is www.webtipstech.com/about-us  and you would like to redirect the user to www.webtipstech.com/privacy-policy then the code that you will use will be: 

Redirect 301 /about-us/privacy-policy

A redirect from a page/post to a new page/post on another website.

  • Redirect 301 /old-post https://www.example.com/new-post

For example, if your old post is www.webtipstech.com/redirect-301 and you would like to redirect the user to www.newwebtipstech.com/301-redirect then the code that you would use would be: 

Redirect 301 /redirect-301 https://www.newwebtipstech.com/301-redirect

A redirect from an old domain to a new domain

  • Redirect 301 / https://www.yournewsite.com/

For example, if your old site is www.webtipstech.com and you would like to redirect the visitors to www.newwebtipstech.com then the code will be:

Redirect 301 /https://www.newwebtipstech.com/

Now that we’re familiar with the basics we can start performing the redirects. Before we start it is essential that we first backup our .htaccess file by downloading it to our computer before making any changes to it. The reason being any mistakes made within this file will make your website inaccessible to you and your visitors so proceed with caution.  

How To Add 301 Redirection Code To .htaccess Using The File Manager 

Step 1: Log into your web hosting account. 

Edit .htaccess Via File Manager Step 1

Step 2: Go to the cPanel and click on the file manager option. 

Edit .htaccess Via File Manager Step 2

Step 3: Navigate through your website’s file structure and locate the public.html folder.

Edit .htaccess Via File Manager Step 3

Step 4: Within the public.html folder you will see the .htaccess file. (This file may be hidden if so you will need to allow hidden files).

Edit .htaccess Via File Manager Step 4

Step 5: Right-click on the .htaccess file and click on edit. 

Edit .htaccess Via File Manager Step 5

Step 5b: Alternatively, you can download the file and edit it using a plain text editor like Notepad, Notepad++, Brackets, Atom, or Visual Studio Code.   

Step 6: Within the .htaccess file under the #END WORDPRESS comment insert one of the three redirect codes we mentioned above for the redirect that you would like to perform.

Edit .htaccess Via File Manager Step 6

Step 7: Repeat step 6 for the remaining redirects. 

How To Add 301 Redirection Code To .htaccess Using FTP 

Step 1: Open your preferred FTP client (Filezilla, WinSCP, Cyberduck, etc.)

Edit .htaccess via FTP step 1

Step 2: Use the quick connect function to connect to your website. 

Edit .htaccess via FTP step 2

Step 3: Locate the .htaccess file by going through the file structure domain-name > public_html.

Edit .htaccess via FTP step 3

Step 4: When you’ve located the .htaccess file within the public_html folder, right-click on it and download it to your computer. 

Edit .htaccess via FTP step 4

Step 5: Open the .htaccess file with your preferred text editor. 

Edit .htaccess via FTP step 5

Step 6: Determine the type of 301 redirects that you would like to do and add the code to the .htaccess file under the #END WORDPRESS comment. 

Edit .htaccess via FTP step 6

Step 7: Repeat step 6 for all the redirect that you would like to do  

Step 8: Return to your FTP client and under the local site section navigate to the folder where you saved the .htaccess file that you added the redirection codes to. 

Edit .htaccess via FTP step 8

Step 9: Right-click on the .htaccess file and click on upload and replace it with the current one on your website.

Edit .htaccess via FTP step 9

Create a 301 Redirect VIA The Functions.php File

To perform a 301 redirect using the functions.php file we will utilize the built-in WordPress function wp_redirect().

With the wp_redirect() function you can perform three different 301 redirects. The first one is a redirection of your entire website to a new one. 

To redirect your entire website you will need to place the following lines of code at the end of your functions.php file. 

wp_redirect( ‘http://yournewsite.com/yournewpost’, 301 );

exit;

For example, if I would like to redirect all my visitors from www.webtipstech.com to www.youtube.com this is the code that I will use at the end of my functions.php file: 

wp_redirect( 'https://www.youtube.com/', 301 );
exit;

With the wp_redirect() function you can also redirect a specific page or post to a new page, post or new website. In order to do this you will need the wrap the wp_redirect() function within a conditional statement. 

The conditional statement that we would use is the following: 

if($_SERVER[‘REQUEST_URI’]==”/old-post”) 

The code above tests to determine if a specific page is being requested. The completed code that you would write in your functions.php file will look like this: 

if($_SERVER[‘REQUEST_URI’]==”/old-post/”){

wp_redirect( ‘http://yourwebsite.com/new-post’, 301 );

exit;

}

For example, if I would like to redirect my users from my about page (www.webtipstech.com/about-us ) to my terms and conditions page (www.webtipstech.com/terms-conditions) this is the code I would use: 

if($_SERVER['REQUEST_URI']=="/about-us/"){
wp_redirect( 'http://www.webtipstech.com/privacy-policy', 301 );
exit;
}

Before we get started editing the functions.php file of your theme we would first need to create a child theme. If you already have a child theme installed you will need to activate it. 

How To Add 301 Redirection Code To Functions.php Using File Manager

Step 1: Log into your hosting provider dashboard. 

Step 2: From the cPanel locate the file manager option and click on it.

Edit Functions.php File via File Manager Step 2

Step 3: Within the file manager navigate to public_html > wp-content > themes > your-theme-child

Edit Functions.php File via File Manager Step 3

Step 4: Backup your functions.php file by downloading a copy of it to your computer.

Edit Functions.php File via File Manager Step 4

Step 5: Right-click on the functions.php file and click edit. 

Edit Functions.php File via File Manager Step 5

Step 6: Under the /*Write here your own functions*/ paste in one of the above redirection codes that fit your needs. (Remember to replace the URLs and slug with the ones that you would like the redirect).

Edit Functions.php File via File Manager Step 6

Step 7: Repeat step 6 for all your other redirects. 

Step 8: When you are done with step 7 save the changes you’ve made.

Edit Functions.php File via File Manager Step 8

How To Add 301 Redirection Code To Functions.php Within WordPress   

Step 1: Log in to your WordPress dashboard.

Step 2: From the menu to the left hover over the “appearance option” and click on “theme editor”.

Edit Functions.php File Within WordPress Step 2

Step 3: In the list of theme files locate the functions.php file and click it. 

Edit Functions.php File Within WordPress Step 3

Step 4: Under the comment line that reads /*Write here your own functions*/ add in the code for the redirect. 

Edit Functions.php File Within WordPress Step 4

Step 5: Repeat Step 4 of the remaining redirects. 

Step 6: When you’ve added all your redirects click on the “update file” button.

Edit Functions.php File Within WordPress Step 6

How To Add 301 Redirection Code To Functions.php Using FTP

Step 1: Open your preferred FTP client and quick connect to your website.

Step 2: Navigate through the file structure domain-name > public_html > wp-content > themes > your-child-theme and locate the functions.php file.

Functions.php File via FTP Step 2

Step 3: Right-click on the functions.php file and download it to your computer.

Functions.php File via FTP Step 3

Step 4: Locate the file on your computer and open it in your preferred file editor.

Functions.php File via FTP Step 4

Step 5: Under the /*Write here your own functions*/ comment paste in the redirection code that you would like to use.

Functions.php File via FTP Step 5

Step 6: Repeat step 5 for all your remaining redirects and when you’re finished save the file.

Step 7: Head on back to your FTP client and under the “Local site” section navigate to where you saved the edited functions.php file.

Functions.php File via FTP Step 7

Step 8: Right-click on the edited functions.php file, click on upload and replace it with the current one on your website.

Functions.php File via FTP Step 8