AJS Digital Group blog

How To Redirect URL Using htaccess

We all bookmark pages all the time. However, we’re sometimes presented with 404 page on return. While this is sad, it’s also bad for a number of reasons.

 

Firstly, you can’t just delete a page on a website. You really should try and send that traffic to another relevant page or at worst a category page. This is just creating a good user experience.

 

Secondly, if you have good quality links pointed at that page, which is helping you to build a stronger domain authority and rankings, then you’ve just shot yourself in the foot! You can kiss goodbye to them. With a redirect in place, you can transfer the link strength onto the new page and keep your website rankings up.

 

Thirdly, redirects can help you use your google crawl budget more effectively. As Google views your website, it will look for pages which it can find (status code 200), have moved (status code 301/302) or have been deleted (status code 404/410) as well as serve errors (status code 500).

 

 

Finding errors

You can use a tool like ScreamingFrog (free) to find out status codes of all your pages. Google Search Console (free) will show you 404 and 500 in an easy to understand way. This is super useful, so start here. So, How To Redirect URL Using htaccess?

 

 

Redirect types and usage

Before I show you how to redirect using htacess and how to check that they’re working, let’s look at a few examples.

 

Former product

So you have a few products which have been discontinued and you want to delete their pages. Just go for it? Wrong.

 

Firstly, consider where the customer intent lies. Do they want support for that product which is answered by another page? Have you decided that product isn’t a fit for your business? or did you find that the product wasn’t profitable under market pressures?

 

In all of these, you can help the customer in their journey by using a 301 redirect.

 

If the product doesn’t fit with your current range or wasn’t profitable then it might be best to redirect to the top category page. In both cases, you’re aiming to keep the prospect warm to your current offering.

 

However, if the product is discontinued but you still provide support, then either redirect to a page about product support (you could also build one!) or add who to contact on your FAQ page.

 

Old pages

We’ve all create pages which we believe target a keyword or a cluster, only to find out later that the keyword is super competitive or doesn’t matter our customer’s intent.

 

In some of these cases, it’s best to delete and start again and redirect the old page to the new one. You can, of course, keep the old page but rename it and rewrite the content to better match the viewer’s intent. Again, you’d want to redirect the old URL to the new.

 

In-depth content

As we trend toward more long-form content, you can answer a few questions in one post and so you might be able to delete a few pages and replace with one more in-depth article. Again redirecting and deleting is your best option.

 

 

How to redirect

Before we get into how to edit your htacess file, a warning! Editing this file can have massive implications on your website. So, if you’re going to try editing this file yourself, please make a copy on your hard drive first. This way if you manage to crash your website, then you’ll be able to get it back up and running quickly.

Ok warning over.

 

Redirect a website to www

This is the simplest redirect

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

 

Redirect to https

If you have an SSL then you’ll want to consider redirecting your website to https

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

 

Simple 301 redirect

The most basic and common redirect you’ll need is a 301

Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html

or

Redirect 301 /oldpage.html http://www.yoursite.com/blog/

 

 

A catch-all (most) redirect

This is quite complex and so you’ll want to spend a few minutes thinking before you go gung-ho and crash your website!

 

If you’re using WordPress you’ll know if you create more than 10 posts, then WordPress automatically starts to create pages with 10 results. If you decided to delete this category then you can’t simply use a 301 redirect as page 2/3/etc will still be indexed by Google and may even be bookmarked.

 

So you’ll need to use 301 redirect match in order to catch all and redirect to a new page. This type is also logic based, which means you can use /*/ to catch things such as multiple pages etc.

 

RedirectMatch 301 ^/blog/category/*/ http://www.yousite.com/newpage.html

 

 

Testing

So you’ve copied your current htaccess file into a word doc or notepad file, what’s next? Copy all your old URLs into a new notepad file. Add in the new URLs after the old as well as the redirect code at the start of the line.

 

Next, you’ll want to add a new redirect to your htaccess file. In order to check that it works and your website is live, open a private tab in your browser and enter your URL. A private window is required as it doesn’t cache your settings/cookie/page.

 

If it works, then you’ve done well. If not, then you need to revisit your code, tweak and try again.

 

Please, add one redirect at a time and keep testing.

 

 

I hope that enables you to create a better user experience and hold on link juice. What is your favourite htaccess redirect?

AJS Digital Group - Digital Marketing, Done Right.

Book your free phone consultation today.