B4b4j1
B4b4j1

Reputation: 460

Redirect sub-folders to a new folder in WordPress

I have been struggling to redirect old urls to the new website pages.

Goal is to redirect many sub-sub folder pages like : https://www.example.com/news/events/event18

to a unique folder : https://www.example.com/focus

Using htaccess file in Wordpress, i tried :

RedirectMatch 301 /oldfolder1/oldpage1/(.*) /newfolder/$1

Result is:

https://www.example.com/newfolder/oldpage1

Another method:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^oldfolder/(.*)$ https://www.example.com/newfolder [R=301,L]

Result is the same !

I have ton of old urls and I want to avoid the manual Redirect 301 for all ot them.

Thx for help.

Upvotes: 0

Views: 74

Answers (1)

Justin R.
Justin R.

Reputation: 488

Is everything going to come out of /news? Or is it going to come out of an additional folder as well? Example below would check first tier subfolder for one of two names (news or articles) before doing rewrite.

RewriteCond %{DOCUMENT_ROOT}/news%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/news%{REQUEST_URI} -d
RewriteRule ^(.*)$ /focus/$1 [L]

# then check if request is in a different folder like /articles/
RewriteCond %{DOCUMENT_ROOT}/articles%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/articles%{REQUEST_URI} -d
RewriteRule ^(.*)$ /projects/$1 [L]

# otherwise, blindly rewrite to /news (or do nothing by removing this rule to allow a 404 not found)
RewriteCond ${REQUEST_URI} !^/news/
RewriteRule ^(.*)$ /news/$1 [L]

Upvotes: 1

Related Questions