简体   繁体   中英

How should I do a 301 redirect / rewrite on my WordPress site?

I am migrating my WordPress website from a sub-domain to the main domain:

So from blog.example.net to example.net .

A typical post URL on the sub-domain would be:

https://blog.example.net/mypost

A typical post on the new domain would be the same:

https://example.net/mypost

The WordPress permalink settings are the same on both. If I add this Rewrite rule to my .htaccess file on blog.example.net :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.example.net [NC,OR]
RewriteRule ^(.*)$ https://www.example.net/$1 [L,R=301,NC]

then page is redirected to:

https://www.example.net/YYYY/MM/DD/mypost

and the page is not found on the new domain:

How do I remove the /YYYY/MM/DD/ from the URL on https://example.net/mypost ?

Try this if there is a date in URI like /2018/03/07 :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?blog.example.net [NC]
RewriteCond %{THE_REQUEST} \s/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)\sHTTP.*$
RewriteRule ^   https://www.example.net/$1 [L,R=301]

If it is exactly like YYYY/MM/DD , replace with this :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?blog.example.net [NC]
RewriteCond %{THE_REQUEST} \s/[Y]{4}/[M]{2}/[D]{2}/(.*)\sHTTP.*$
RewriteRule ^   https://www.example.net/$1 [L,R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM