Score:1

How to replace custom url old domain to new domain with .htaccess

er flag

How to replace all url entire website with new domain and new permalink? I have 5k url to replace at my site.

Example :

  • Current URL: oldomain.com/2020/12/data-raid-4963.html
    Replace To: sub.newdomain.com/4963-2/

  • Current URL: olddomain.com/2019/09/data-raid-2125.html
    Replace To: sub.newdomain.com/2125-2/

kz flag
Is that last `-2` fixed for all URLs? Do these two domains/hostnames point to different servers?
Maone van Cobain avatar
er flag
Yes fix for all URLs and two domain/hostname point is same servers.
Score:0
kz flag

Since the domains point to the same server, you would need to do something like the following using mod_rewrite at the top of the root .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^20\d\d/\d\d/[a-z-]+-(\d+)\.html$ https://sub.newdomain.com/$1-2/ [R=301,L]

I've assumed the data-raid part of the URL-path can consist of any sequence of 1 or more of a-z (lowercase) or - (hyphen) characters only. If this is always literally data-raid (as per your examples) then replace [a-z-]+ accordingly in the regex.

The $1 backreference contains the digit sequence immediately before the .html at the end of the URL-path. In other words, the contents of the captured subpattern (\d+).

You should test first with a 302 (temporary) redirect to avoid potential caching issues.

Reference:

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.