Permalink Redirection and Regular Expressions in WordPress

丶灬走出姿态 提交于 2019-12-08 08:50:58

问题


I've got a pretty basic RegEx question but it's pressing enough that I don't have time to run down a tutorial on RegEx in order to answer it myself.

In short, I need: /2009/01/28/post-name/

redirected to /post-name/

As well as /post-name/author-name/

redirected to /post-name/

I promise to go take a class in RegEx before the month is out. Deal?

PS. Yes, I know that the Wordpress Redirection plugin will automate .htaccess but it won't write RegEx for me.


Solved using the Redirection plugin.

Source URL: /(\d*)/(\d*)/(\d*)/([A-Za-z0-9-]*) Target URL: /$4


回答1:


As this may be found by someone in the future, I'm going to answer in as much detail as possible. I actually posted a blog on the subject here where I could discuss it.

The simplest solution uses very basic regex in concert with the Redirection plugin for WordPress. In this instance, you can use simple structures:

  • (\d*) for a numeric string
  • ([A-Za-z0-9-]*) for an alphanumeric string
  • $# (in my case $4) to determine which of the outputs to keep
  • a / for the directory seperator

So in the case of /yyyy/mm/dd/post-name we use:

  • Source URL: /(\d*)/(\d*)/(\d*)/([A-Za-z0-9-]*)
  • Target URL: /$4

in order to output /post-name as a 301.

Don't forget to check the regex box!



来源:https://stackoverflow.com/questions/18774545/permalink-redirection-and-regular-expressions-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!