Buddika Nishantha
Buddika Nishantha

Reputation: 53

Wordpress Redirect old URL to new

I want to redirect my old web site post in to new website. I can't write one by one because there have more than 1000 post. Main problem is new url have post Id after post name.

Old website url

https://www.old.com/economy/test-test/

New website url

https://www.new.com/test-test-2001/

I tried this but can't add post id correctly

RewriteRule ^economy/(.*) https://new.com/$1-2001 [R,L]

Upvotes: 1

Views: 162

Answers (1)

MrWhite
MrWhite

Reputation: 45829

You don't state what the "error" is exactly, but from the example URL given - which ends in a trailing slash - your directive will result in a redirect to /test-test/-2001, which is different to the expected URL of /test-test-2001/ (you are also missing the trailing slash on your directive).

Try the following instead - this needs to go before the WordPress front-controller, if not already.

RewriteRule ^economy/([^/]+) https://new.com/$1-2001/ [R,L]

HOWEVER, if 2001 is the post ID that relates specifically to this post title (slug) of "test-test" then you can't do a "wildcard" redirect like this in .htaccess since all requests for /economy/<something>/ will naturally be redirected to /<something>-2001/.

If that's the case then this is probably better done in WordPress itself (processed late in order to not impact site visitors). Otherwise you will need to create individual redirects for each post. For example:

RewriteRule ^economy/(test-test) https://new.com/$1-2001/ [R,L]

The $1 backreference simply saves repetition if the post title is the same on the new site.

Upvotes: 1

Related Questions