Search Marketing, WordPress

htaccess: Strip Folder and Redirect with Regex

Simplifying your URL structure is a great way to optimize your site for a number of reasons. Long URLs are difficult to share with others, can get cut off in text editors and email editors, and complex URL folder structures can send the wrong signals to search engines on the importance of your content.

If your site had two URLs:

  • https://martech.zone/blog/category/search-engine-optimization/htaccess-folder-redirect-regex OR
  • https://martech.zone/htaccess-folder-redirect-regex

Which one would you think has provided the article with higher importance? The first example has a separation between the article and the home page of 5 levels. If you were a search engine, would you think this was important content?

For these reasons, we simplify many of our clients’ folder structures. Some might argue that a couple of category slugs with keywords are better, but we’ve not seen this with our clients. The hierarchy and number of links from the home page have driven rankings far better with our popular content.

After implementing a blog, though, it’s a bit of a pain to undo all of these permanent links and still properly redirect traffic from existing links out there to the new URL structure. With Flywheel (affiliate link), we can have their team manage our redirects or we can use the redirection plugin.

  1. First, we employ Yoast’s WordPress SEO plugin so that we can strip the literal category slug out of the URL.
  2. Next, we update permalinks and remove /%category%/ and just leave /%post%/ in the field (and refresh the cache).
  3. Last, we have to add a regular expression to properly redirect the folder:

strip-folder-redirect-regex

The expression has your optional categories listed (folder1, folder2, folder3) and requires some text after the category… this way your category pages won’t break but the independent articles will forward properly to the new URL.

^/(folder1|folder2|folder3)/([a-zA-Z0-9_.-]+)$

I’m not sure I’d recommend this drastic change for every company. Those with an established ranking may not wish to deploy this. In the short term, it could hurt your ranking since a redirect doesn’t carry all the authority of the original page. But over time, having more content higher in your permalink hierarchy could help you. We know it’s helped the MarTech Blog!

Leave a Reply