Some Time Saving .htaccess Redirect Directives
This article was originally posted on January 18, 2022.
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
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
Update: This would redirect all of the URLs above to https://yourwebsite.com/our-staff although it would also redirect urls like this to https://yourwebsite.com/our-staff.
- https://yourwebsite.com/sub-1/we-are-excited-to-welcome-tom-smith
- https://yourwebsite.com/sub-1/sub-2/we-are-excited-to-welcome-mary-jane
- https://yourwebsite.com/sub-1/sub-2/sub-3/we-are-excited-to-welcome-spider-man
If you wanted to be more specific and target urls that contain similar text at the beginning of them, you need to add the ^ symbol like so:
RedirectMatch301 ^/we-are-excited-to-welcome https://yourwebsite.com/our-staff
If you wanted redirect several urls to a single url that had the same subdirectory like the following to a single url:
- 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
You could use this RedirectMatch directive:
RedirectMatch301 ^/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.