• Resolved aryanduntley

    (@dunar21)


    I have recently discovered that 301 redirects are auto-generated by wordpress. I cannot find WHERE that is taking place. It must be stored in the database somewhere. Post meta has _canonical for some posts and _wp_old_slug for others. Options table has rewrite_rules entry. But I cannot find anywhere in the database where the old permalink or category / taxonomy for a given post is referenced in order to create the redirect. Does anyone know where this is taking place?

    I would like to have better control over the redirects, view what is already there, remove anything that shouldn’t be there or might not need to be there anymore, add, edit, whatever. For the most part, I probably won’t need to add or edit, but it’s likely I may have to remove some. There have been database transfers, restores, and so on through the years and if this has been creating these redirects since the site was first put up, then it’s likely in serious need of cleanup.

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’m afraid I don’t understand what kind of redirects you are looking for exactly? Is it about categories whose slugs have changed?

    For an overview of URL redirects you might be interested in one of the plugins here: https://www.ads-software.com/plugins/tags/redirect/

    Moderator bcworkz

    (@bcworkz)

    You’re probably seeing the result of redirect_canonical(). The redirect occurs when the request resolves to a particular object, but the request is not its canonical URL. The canonical URL is determined from wp_get_canonical_url(), which typically determines it from get_permalink(), at least in the case of post requests.

    The permalink doesn’t come from any one specific place in the DB, it’s built dynamically from the currently defined permastruct, with its %{tag-name}% elements being populated with various properties of the requested object.

    Thread Starter aryanduntley

    (@dunar21)

    Ok. The question relates to a changed taxonomy. A post with a category, for instance /cars/sports/kia-rio.htm is changed to /cars/sedans/kia-rio.htm. WP automatically creates the redirect from the old to the new url… I was thinking this HAD to be stored in the database. The old state would have to be known in order to create a redirect. But after reviewing the code for redirect_canonical in canonical.php, it appears that:

    elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) {	                        $category_name = get_query_var( 'category_name' );		                        if ( $category_name ) {	                                $category = get_category_by_path( $category_name );		                                if ( ! $category || is_wp_error( $category )	                                        || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() )	                                ) {	                                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );	                                        $redirect_obj = get_post( $wp_query->get_queried_object_id() );	                                }	                        }	                }
    
    

    it splits up the url into query parameters, checks to see if the post category exists and if so then checks whether the it’s correct. If it’s not, then it just redirects to the correct permalink. Meaning that no past state is saved in the database.

    Is this correct? Or am I still missing something?

    Thread Starter aryanduntley

    (@dunar21)

    @bcworkz Yes. Thank you. After I submitted my last response, I read your reply again and you described it exactly that way. The request is created from the post object. The post object is determined by the slug. The permalink is generated and it sends the request to the right place. It does this by way of a 301.

    Thanks for pointing me in the right direction.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Category or taxonomy change for a post 301 redirection management’ is closed to new replies.