Vladimír Paraska
Vladimír Paraska

Reputation: 47

Regex redirect of wordpress blog from subdomain to domain

I am trying to redirect the WordPress blog post from old to new domain. We had a blog on subdomain http://blog.domain.xyz/ and after the migration is on main domain https://www.domain.xyz/

On the old blog, URL of the blog post was:
http://blog.domain.xyz/2020/03/25/post-name   (part /2020/03/25/ is just an example of date)
now I need it to redirect to:
https://www.domain.xyz/post-name

I matched with regex domain and date part:

http\:\/\/blog.domain.xyz\/\d{4}\/\d{2}\/\d{2}\/

I know how to redirect manually all posts one by one, but there are more than 1000 posts, so this is not an option.

I can't figure out how to take post-name part and apply it to the new domain

Upvotes: 0

Views: 269

Answers (2)

Vladimír Paraska
Vladimír Paraska

Reputation: 47

In the meantime I figured it out:

<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^blog.domain.xyz$ RewriteRule \d{4}/\d{2}/\d{2}(.*)$ https://www.domain.xyz$1 [R=301,L] </IfModule>

Upvotes: 0

mikerojas
mikerojas

Reputation: 2338

I think you need something like the following.

RewriteRule ^/\d+/\d+/\d+/(.*)$ https://www.domain.xyz/$1 [R=301,L]

// ^/ start at the root
// \d+/\d+/\d+/ match date folders like 2020/03/25/
// (.*) the part we want to keep "some-slug"
// $ end of match
// $1 put the part we want to keep here "some-slug"

Basically matching any urls with /2020/03/25/post-name and redirects to https://www.domain.xyz/post-name.

Note: this assumes you're adding the redirect at the old domain.

Upvotes: 1

Related Questions