Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top