最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Need Help Correct Regular Expression Redirect Code

programmeradmin2浏览0评论

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

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

Share Improve this question asked Nov 17, 2020 at 15:00 kalkunkalkun 234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论