How To Do a 301 Redirect On WordPress Without a Plugin

You can do a 301 redirect on a WordPress website without using a plugin by adding a few lines of redirection code to either the .htaccess or functions.php files of the website.  

In this article, I will provide an in-depth tutorial on performing a 301 redirect using the .htaccess and functions.php files of your WordPress website.

301 Redirect WordPress Without Plugin

How To Do a 301 Redirect Using 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 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 you would like to redirect. (If you fail to provide the URL-path, the redirection will fail).
  • URL: This is the page or post you would like to redirect the user to. (If you fail to provide the URL, the redirection will fail).

There are three types of 301 redirects that you can perform using the .htaccess file. These are:

1 – 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 want to redirect the user to www.webtipstech.com/privacy-policy, then you would use the code: 

Redirect 301 /about-us/privacy-policy

2 – 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 want to redirect the user to www.newwebtipstech.com/301-redirect, then you would use the code:  

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

3 – 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 you would use the code:

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

How To Add 301 Redirection Code To The .Htaccess File Using the Cpanel 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: Within the file manager, navigate to the public.html folder.

Edit .htaccess Via File Manager Step 3

Step 4: Within the publuc.html folder, find the .htaccess file, right-click on it and click edit. (If you don’t see the .htaccess will need to enable the show hidden files setting in your file manager).

Edit .htaccess Via File Manager Step 4

Step 5: Under the #END WordPress comment in the .htaccess file, insert the redirection code for the 301 redirects you would like to perform.

Edit .htaccess Via File Manager Step 6

Step 7: Repeat step 5 for your remaining redirects.  

How To Add 301 Redirection Code To The .Htaccess Using FTP 

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

Edit .htaccess via FTP step 1

Step 2: Connect to your website. 

Edit .htaccess via FTP step 2

Step 3: Navigate to the public_html folder and locate the .htaccess file. 

Edit .htaccess via FTP step 3

Step 4: Right-click on the .htaccess file 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: Within the .htaccess file, add the 301 redirection code for the redirect you want to perform under the #END WordPress comment.  

Edit .htaccess via FTP step 6

Step 7: Repeat step 6 for all the redirects you want to do and save the file.   

Step 8: Return to your FTP client

Step 9: Under the local site section, navigate to the folder where you saved the edited .htaccess

Edit .htaccess via FTP step 8

Step 10: Right-click on it, then click upload to replace it with the current one on your website.

Edit .htaccess via FTP step 9

Create a 301 Redirect Using The Functions.php File

You can use the wp_redirect() WordPress function to perform a 301 redirect using the functions.php file.

There are two types of 301 redirects that you can perform using the wp_redirect() function. These are: 

1 – A 301 redirection of your entire website to a new website.

To redirect your entire website, 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 of my visitors from www.webtipstech.com to www.youtube.com, this is the code that I will use:  

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

2 – A 301 redirection of a specific page or post to a new page, post, or website.

To do this, you will need the wrap the wp_redirect() function within the conditional statement if($_SERVER[‘REQUEST_URI’]==”/old-post”)

The conditional statement above tells the function it should redirect the requested page or post. 

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, I would write: 

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

Before you edit the functions.php file of your theme, you will need to create and activate a child theme

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 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*/ comment, 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 and save the file.

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 your mouse pointer over the appearance and click 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 connect to your website.

Step 2: Navigate 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 you want 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: Return 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
You May Also Like