Need Help Correct Regular Expression Redirect Code

I have custom permalinks with this code

/%year%/%monthnum%/%postname%.html

and now switching to another custom permalinks

%postname%.html

so obviously all my content is now 404 not found, in yoast redirect setting there are Correct Regular Expression Redirect options or I could use htaccess to redirect my old content

anyone can help me with the code or suggestion for redirecting via yoast or htacces ??

Thank you

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

At the top of your .htaccess file, before your existing WordPress directives, you could do something like the following to redirect the old permalink:

RewriteRule ^d{4}/dd/([w-]+.html)$ /$1 [R=301,L]

The RewriteRule pattern matches against the URL-path less the slash prefix.

d{4}/ – matches the 4-digit year, followed by a slash.

dd/ – matches the 2-digit month number, followed by a slash.

([w-]+.html) – matches the postname and .html extension. The surrounding parentheses make this into a capturing group which is then referenced in the substitution string with the $1 backreference. [w-] matches characters in the range a-z, A-Z, 0-9, _ (underscore) and - (hyphen). If your postname can contain any other characters then these will need to be added to this character class (although the hyphen must appear last).

Test with a 302 (temporary) redirect to avoid caching issues in case anything goes wrong. 301 (permanent) redirects are persistently cached by the browser by default.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x