Search code examples
apache.htaccesshttp-redirectmod-rewrite

I changed domains and post slug structure at the same time for my WP site. Can I use 1 redirect to do so with htaccess?


I am planning a domain change from example1.com to example2.com. To add a twist to it, I also want to change my permalinks at the same time. My current permalinks for posts have the date and I want to remove it.

I'm a bit hesitant to test and lose SEO so I was hoping someone could confirm this would work before.

Here is what I was thinking: after changing domains I use this code in my htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example1.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com [NC]
RewriteRule ^\d{4}/\d{2}/(.*) https://example2.com/$1 [R=301,L]

then I found this rule to change dates:

RewriteRule ^[0-9]{4}/[0-9]{2}/(.*)$ https://example2.com/$1

I saw this one as well:

RewriteRule  ^/(\d*)/(\d*)/([A-Za-z0-9-]*)$ https://example2.com/$4

I'm not sure what these rules specifically mean but I THINK I should be able to combine them like this?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example1.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com [NC]
RewriteRule ^[0-9]{4}/[0-9]{2}/(.*)$ http://example2.com/$1 [L,R=301,NC]

It doesn't seem quite right.

Or would simply changing the permalink structure in WordPress affect the change so that

https://www.example1.com/2019/01/how-to-write-about-cars/
redirects to
https://www.example2.com/how-to-write-about-cars/

UPDATE

Using MrWhite's answer below. I added this code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example1.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com [NC]
RewriteRule  ^/(\d*)/(\d*)/([A-Za-z0-9-]*)$ https://example2.com/$4

This is working now in the case of

https://www.example1.com/2019/01/how-to-write-about-cars/
which redirects to
https://www.example2.com/how-to-write-about-cars/

However

https://www.example2.com/2019/01/how-to-write-about-cars/
does NOT redirect to 
https://www.example2.com/how-to-write-about-cars/

It just returns a 404. This likely isn’t an issue as nothing should be bookmarked but just in case, is there a way to fix that?


Solution

  • Or would simply changing the permalink structure in WordPress affect the change

    I don't believe this would implement the redirect from the old to new URL structure, if that is what you are thinking. (At least not by default.)

    RewriteCond %{HTTP_HOST} ^example1.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.example1.com [NC]
    RewriteRule ^[0-9]{4}/[0-9]{2}/(.*)$ http://example2.com/$1 [L,R=301,NC]
    

    This looks OK. Although if the new URLs at example2.com don't contain the date (ie. /YYYY/MM/ prefix) then there wouldn't seem to be any need to check the requested hostname.

    This rule must also go at the top of the .htaccess file, before any of the existing WordPress directives (ie. before the # BEGIN WordPress comment marker).

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

    Final Solution

    This can, however, be tidied a bit. The following one-liner should be sufficient:

    RewriteRule ^\d{4}/\d{2}/(.*) https://example2.com/$1 [R=301,L]
    

    You do not need any of the RewriteCond directives. (Just the RewriteEngine On directive, if it doesn't already appear elsewhere in the .htaccess file.)

    Note the https on the target URL. \d (shorthand character class) is the same as [0-9]. The trailing $ on the regex is not required since regex is greedy by default. The NC flag is not required either, since there is nothing case specific in this regex.


    Aside: (Don't use this!)

    I saw this one as well:

    RewriteRule  ^/(\d*)/(\d*)/([A-Za-z0-9-]*)$ https://example2.com/$4
    

    This rule, however, is very wrong! Due to the slash prefix on the RewriteRule pattern this will never match in .htaccess and the rule will do nothing. But there are only 3 capturing groups in the regex, so the $4 backreference would always be empty (everything would be redirected to the homepage, which would likely be treated as a soft-404 by search engines).