Divi WordPress Theme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix Reply To: Redirect help

Discussion in 'Misc WordPress Requests' started by bcworkz, Feb 11, 2018.

  1. bcworkz


    Reply To: Redirect help, by bcworkz

    You should find that just about any URL ending with a post slug will return the single post and redirect to it’s permalink. If a valid post slug is my-first-post, requesting example.com/terms/that/mean/nothing/to/wp/my-first-post/ will redirect to the current post’s permalink. Where we run into trouble is if any of those terms do mean something to WP, the request will likely 404. For example, numeric terms rarely work because WP will try to interpret them as date parameters. Chances are they will not match the post date, so no match will be found. Such automatic redirects return with 301 status, so search engines should properly handle them.

    But if you feel explicitly making redirects will be beneficial, there’s no reason not to. The regexp syntax is only accepted for source URLs. Target URLs must be composed of a combination of fixed string literals and capture groups from the source regexp. There’s no opportunity to insert variable data from the DB, such as a post’s date, if it is not available in the source URL.

    To get to URLs that contain data that is not from the source, the source needs to be rewritten to query vars. With the right query vars, WP will find the right post and redirect to the established permalink, using DB data to fill in any blanks.

    For example, if your source URL is /index/id/1234/, you cannot rewrite to /02/03/2018/my-thousandth-post/ There is no date data or slug data in the source. The necessary data does not exist. What you can do is rewrite to index.php?p=1234 because 1234 can be captured from a regexp. WP will then find post 1234 and redirect to the established permalink /02/03/2018/my-thousandth-post/ for you. You don’t have to use post ID in rewrites, only something that WP can use to get to a single post. It can be the post slug or a set of meta data that resolves to a single post. But it has to be available in the source. The only data that can be created are string literals that do not change for a particular regexp match.

    Writing regexps that work is certainly a challenge. I suggest you work out what you need using any one of several regexp “fiddle” tools (such as regexr.com) where you can easily try different regexps until you arrive at one that does the job.

    Reply To: Redirect help

Share This Page

Monarch Social Sharing Plugin