How to Create 301 Redirects with .htaccess
A 301 redirect in your .htaccess file lets search engines and others know that you have replaced an old link with a new one. It is one of the best methods to direct traffic from an old page. If you get lots of traffic to a specific page but you have removed that page, you wouldn’t want them to still go there and see a ‘404 error’, you would want them to go to another page on your website. If these users get a 404 error they are more likely to leave the website than stay and browse.
You don’t need much coding knowledge to access and edit your .htaccess file. Make sure that you do save a backup just in case something does go wrong.
How To Access Your .htaccess File & Create A Redirect
- Login to your Hosting
- Navigate to your cPanel
- Click File Manager
- Find your .htaccess file – you may need to reveal your hidden files to find it. With many File Managers, you can simply go to Settings and tick “Show Hidden Files”
- Before you access your .htaccess file, make sure that you save a backup – you should be able to right-click then click Download to save it to your computer
- After you have saved a backup of your .htaccess file, right-click again then click Edit (in File Manager)
- This is where you will add the code for your redirects. See below for the code. Make sure you add this code to the bottom of the file.
Redirect 301 /old-url /new-url
Test The Redirect
In your browser, go to yourwebsite.com/old-url. It should automatically point you to yourwebsite.com/new-url. This means that the redirect update you made to your .htaccess file worked.
If you wish to add more redirects you can. Just use the code above and put it under the first redirect that you created.
If the redirect doesn’t work, check that the URL’s are correct. A reason for this not working is you could have put:
Redirect 301 https://www.yourwebsite.com/old-url https://www.yourwebsite.com/new-url
If you do this it may not work. To ensure that it does work make sure you only use the URL slug after the forward slash (/) when editing the code.
Redirect To A Different Domain
You can easily redirect a URL to a different domain by changing the code slightly. This is worth doing if you need to direct traffic from an old website to a new one.
Add the below code to your .htaccess file:
Redirect 301 /old-url https://www.yournewwebsite.com/new-url
This redirect code is slightly different to the one before, where you would redirect from a page on your existing site to another page. With this code you will only need the URL slug for your old website, then the full link for the new website.
If you have any issues, leave a comment below and we will help you!