Some Time Saving .htaccess Redirect Directives

When providing SEO services, we often redirect pages that no longer exist to working pages. We also redirect pages who’s URLs have changed. A basic redirect directive is a 301 redirect.
Redirect 301 /page-name/ https://yourwebsite.com/another-page
This will redirect https://yourwebsite.com/page-name/ to https://yourwebsite.com/another-page/. It was also redirect https://yourwebsite.com/page-name/yo to https://yourwebsite.com/another-page/yo.
How to Redirect an Entire Website to a New URL
If nothing has changed on your website except for the domain name, you would use this redirect directive in the .htaccess file of the old domain’s root directory.
#Place this in the .htaccess file in the root directory for youroldwebsite.com
Redirect 301 / https://yournewwebsite.com
This would redirect the following:
- https://youroldwebsite.com/page-1 to https://yournewwebsite.com/page-1
- https://youroldwebsite.com/page-2/subpage-1 to https://yournewwebsite.com/page-2/subpage-1
- and so on…
How to Redirect Multiple URLs That Contain Similar Text at the Beginning of Them
What if there were multiple non-existing pages that you needed to redirect like this?
- https://yourwebsite.com/we-are-excited-to-welcome-tom-smith
- https://yourwebsite.com/we-are-excited-to-welcome-mary-jane
- https://yourwebsite.com/we-are-excited-to-welcome-spider-man
- https://yourwebsite.com/we-are-excited-to-welcome-incredible-hulk
- and so on…
If there were let’s say 100 urls like this, it would be quite time consuming process to create a 301 redirect for each of these urls.
That’s where RedirectMatch comes in.
Using the example above, one RedirectMatch directive would take care of this.
RedirectMatch301 /we-are-excited-to-welcome https://yourwebsite.com/our-staff
This would redirect all of the URLs above to https://yourwebsite.com/our-staff. If you had to redirect several URLs that no longer existed anymore like this:
- https://yourwebsite.com/old-services/service-1
- https://yourwebsite.com/old-services/service-2
- https://yourwebsite.com/old-services/service-3
- https://yourwebsite.com/old-services/service-4
- and so on…
You would just add a forward slash after the URL in the RedirectMatch directive like so:
RedirectMatch 301 /old-services/ https://yourwebsite.com/new-services/
How to Redirect Multiple URLs That Contain a Query in Them
To redirect non-existing URLs with a query in them like this to another page:
- https://yourwebsite.com/?client=company-one
- https://yourwebsite.com/?client=company-two
- https://yourwebsite.com/?client=company-three
- https://yourwebsite.com/?client=company-four
- and so on…
You would use a Mod_Rewrite like this:
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^client [NC]
RewriteRule ^(.*)$ https://yourwebsite.com/$1? [R=301,L]
</IfModule>
This would redirect all of the above client query urls to the home page.
I hope this has helped you. I’m not an .htaccess expert and regular expressions, but I am learning. As I learn more, I will update this blog post.
If you felt like this blog post has helped you, let me know in the comments.