0
votes

The old domain initially had a permalink structure that goes domain1.com/%year%/%month%/%date%/%post-id%/post-title and while I changed the permalink structure to something simple domain1.com/post-title later on, the redirection was as simple as installing a redirection plugin.

But recently, the website migrated to a new domain; let's call it domain2.com. The following code has been added in .htaccess to redirect all old URLs to their corresponding URLs on new domain:

RewriteCond %{HTTP_HOST} ^domain1.com$ [OR]        

RewriteCond %{HTTP_HOST} ^www.domain1.com$         

RewriteRule ^(.*)$ https://domain2.com/$1 [R=301,L]

This code works for most pages, except for the ones using old permalink structure.

The old permalink URLs are causing too many redirections (old domain name with old permalink structure -> old domain name with new permalink structure -> new domain name with new permalink structure), which I don't think is good for SEO.

The pages have good backlinks, and so the goal here is to pass on the value to the new domain.

Now, I understand that I can use this code for individual URLs (credit MrWhite)

RewriteCond %{HTTP_HOST} (w*)domain1\.com$ [NC]
RewriteRule ^2020/10/25/2257/title/?$ https://domain2.com/title/ [L,R=301]

But as that would involve adding in a lot of lines, I'm curious if this can be done with a generic piece of code -- old permalink URLs of old domain should redirect to the current permalink structure of new domain.

Both domains are hosted on the same server.

1
Certainly that is possible, all you need to do is replace the "title" (whatever that is) in the rule you have with a generic pattern matching all possible such titles. However you will also need to take care that those redirections do not get applied to exissting, valid paths. Which is something we cannot help with, since you do not know anything about your content and resources.arkascha
@arkascha Could you please expand on that and give a replacement code if possible? I'll certainly make sure that it won't affect the existing paths.JustAnotherNoob
You literaly only need to replace "title" with a simple regular expression matching your titles. I do not know your titles, you did not tell us. If you struggle with the regular expression itself I suggest you work though one of the endless "getting started" tutorials about exactly that topic. You need to be able to handle the tools you use yourself, such are the rules here. And you will benefit yourself if you give it a try and learn your tools.arkascha
Thanks for the input @arkascha, much appreciated! Found the solution, will update the post :)JustAnotherNoob

1 Answers

0
votes

Found the solution.

If you're in a similar situation (please refer post), just add something like this above the domain-level redirect.

RewriteCond %{HTTP_HOST} (w*)domain1\.com$ [NC]
RewriteRule ^2020/\d+/\d+/\d+/(.+)$ https://domain2.com/$1 [L,R=301]