1

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

1 Answer 1

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}/\d\d/([\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.

\d\d/ - 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.

1
  • 1
    Thank you ..works perfectly on the new URL and archive pages
    – kalkun
    Commented Nov 17, 2020 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.