Redirect rule for URLs ending with the same suffix?

0

So I have a number of pages with URLs end in the same thing but have different content between the main URL and the final suffix. Some of these have multiple levels. For example:

https://www.example.com/category-1/page-to-be-redirected

https://www.example.com/news/topic-1/page-to-be-redirected

https://www.example.com/subfolder/category-1/category-2/page-to-be-redirected

https://www.example.com/blog-news/page-to-be-redirected

I need a redirect rule which will find all of these instances (anything including '/page-to-be-redirected' in this example) and send them all back to the home page (e.g. https://www.example.com).

Any help / advice would be amazing. Thank you!!

Aimee

Posted 2019-06-18T09:06:16.207

Reputation: 1

In which product and operating system? Browser? IIS? Apache? – harrymc – 2019-06-18T10:28:12.543

It's a standard WordPress site, I'm looking for a redirect rule I can add to the .htaccess file as it's running on Apache. – Aimee – 2019-06-18T11:00:08.677

Answers

0

The simplest way to add and manage redirects in WordPress is by using the Redirection plugin. Install and activate the plugin. Once activated, visit Tools » Redirection to setup your redirects.

Documentation for the Redirection plugin can be found in this article.

If you would rather use the web server configuration file .htaccess, you will need to connect to your website using an FTP client. The .htaccess file resides in your WordPress site’s root directory. If you can’t see the file there, you need to set the FTP client to show hidden files. You may edit the .htaccess file in any text editor.

An example of a simple redirect (but much more can be found on the web) :

RewriteEngine On
Redirect 301 ^https://www.example.com/.*/page-to-be-redirected$ https://www.example.com/

For more information, see for example the article
Beginner’s Guide to Creating 301 Redirects in WordPress (Step by Step).

harrymc

Posted 2019-06-18T09:06:16.207

Reputation: 306 093

Thank you. I've used the Redirection plugin previously though, that's not what I need in this instance. I've also used the .htaccess file to add 301 redirects via FTP thousands of times previously.

What I'm looking for is the redirect rule I'd need to solve the issue mentioned above. As mentioned, I have multiple versions of a URL and I need a way to catch them all and automatically redirect them back to the home page. I just can't find the rule I'm looking for that would work when there are (multiple) different levels prior to the section of the slug which reflects the page title. – Aimee – 2019-06-18T12:21:46.153

Apache mod_rewrite supports regular expressions. Is the problem just formulating the regex? – harrymc – 2019-06-18T12:26:47.143

Yeah, sorry... I'm not sure what it is exactly that I need to write when it comes to the actual regex I need to use. :/ – Aimee – 2019-06-19T11:35:33.257

I tried my hand at writing the rule. See my answer. – harrymc – 2019-06-19T13:47:53.593