• Resolved mufimufin

    (@mufimufin)


    Hello,

    first of all I really like your plugin.

    I have a problem, because when I change permastructures and regenerate it only works with the old URL address.

    OLD URL:
    xyz.com/foodmenu/%foodmenu%

    NEW URL:
    xyz.com/menu/%menu%

    The strange thing is that at the same time this works:
    xyz.com/menu/hamburger

    but this does not:
    xyz.com/menu/ = 404 (only old URL xyz.com/foodmenu/ is working)

    There are no URL conflicts. I have tried to translate more slugs, but with the very same result.

    Is there anything I have missed, please?

    Thanks.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Maciej Bis

    (@mbis)

    Hi @mufimufin,

    please confirm, only the new post archive permalink does not work?

    xyz.com/menu/ <==> xyz.com/foodmenu/

    Regards,
    Maciej

    Thread Starter mufimufin

    (@mufimufin)

    Hi Maciej,

    I have not tried any new post.

    The desired behaviour is to translate SLUG for posts that already exist and came with the template.

    I have therefore changed permastructures, hit regenerate on the other tab (verified that these are selected in checkboxes below) and tried the direct access to that URL which resulted in 404.

    What does not work is this:
    xyz.com/menu/ (previously xyz.com/foodmenu/)

    It only responds on the previous URL = the one in brackets.

    Regards

    • This reply was modified 5 years, 4 months ago by mufimufin.
    • This reply was modified 5 years, 4 months ago by mufimufin.
    Plugin Author Maciej Bis

    (@mbis)

    Hi @mufimufin,

    the link that you are referring to is probably a post type archive. Could you try to add this code snippet to functions.php file in your (child) theme directory?

    function pm_detect_post_archives($request) {
    	global $permalink_manager_permastructs, $permalink_manager_options, $wp, $wp_rewrite;
    
    	if(!empty($permalink_manager_permastructs['post_types']) && empty($request['do_not_redirect'])) {
    		foreach($permalink_manager_permastructs['post_types'] as $post_type => $raw_pattern) {
    			$do_not_append_slug = (!empty($permalink_manager_options['permastructure-settings']['do_not_append_slug']['post_types'][$post_type])) ? true : false;
    
    			// Remove the last part
    			$pattern = ($do_not_append_slug) ? preg_replace('/(.*)\/([^\/]+)$/', '$1', $raw_pattern) : $raw_pattern;
    
    			// Remove the dynamic parts
    			$pattern = preg_replace('/((?:[^\/]+)?(?:%[^\/]+%)(?:[^\/]+)?)/', '', $pattern);
    
    			// Remove duplicated slashes
    			$pattern = trim(preg_replace('/(\/+)/', '/', $pattern), '/');
    
    			// Escape slashes
    			$pattern = preg_quote($pattern, "/");
    
    			preg_match("/^\/?{$pattern}(?:(\/{$wp_rewrite->pagination_base})\/([\d]+))?\/?$/mi", trim($wp->request, '/'), $parts);
    
    			if(!empty($parts[0])) {
    				$request = array(
    					'post_type' => $post_type
    				);
    
    				// Suport pagination
    				if(!empty($parts[2])) {
    					$request['paged'] = $parts[2];
    				}
    
    				return $request;
    			}
    		}
    	}
    
    	return $request;
    }
    add_filter('request', 'pm_detect_post_archives', 99);

    If this does not work, please send me the URL to your website to my email address: contact /at/ maciejbis.net.

    Thread Starter mufimufin

    (@mufimufin)

    It worked! Thank you.

    The only problem I need to solve now is how to get rid of previous URL`s that are still working (e.g. /foodmenu responds). It may represent a problem in terms of SEO.

    Plugin Author Maciej Bis

    (@mbis)

    Please use this extended code instead. It contains additional snippet that filters get_post_type_archive_link() function:

    function pm_detect_post_archives($request) {
    	global $wp, $permalink_manager_permastructs, $wp_rewrite;
    
    	if(!empty($permalink_manager_permastructs['post_types']) && empty($request['do_not_redirect'])) {
    		foreach($permalink_manager_permastructs['post_types'] as $post_type => $permastructure) {
    			$pattern = pm_get_post_type_archive_base($post_type);
    
    			// Escape slashes
    			$pattern = preg_quote($pattern, "/");
    
    			preg_match("/^\/?{$pattern}(?:(\/{$wp_rewrite->pagination_base})\/([\d]+))?\/?$/mi", trim($wp->request, '/'), $parts);
    
    			if(!empty($parts[0])) {
    				$request = array(
    					'post_type' => $post_type,
    					'do_not_redirect' => 1,
    				);
    
    				// Suport pagination
    				if(!empty($parts[2])) {
    					$request['paged'] = $parts[2];
    				}
    
    				return $request;
    			}
    		}
    	}
    
    	return $request;
    }
    add_filter('request', 'pm_detect_post_archives', 99);
    
    function pm_get_post_type_archive_base($post_type) {
    	global $permalink_manager_permastructs, $permalink_manager_options;
    
    	if(!empty($permalink_manager_permastructs['post_types'][$post_type])) {
    		$do_not_append_slug = (!empty($permalink_manager_options['permastructure-settings']['do_not_append_slug']['post_types'][$post_type])) ? true : false;
    
    		// Get the permastructure
    		$post_type_archive_slug = $permalink_manager_permastructs['post_types'][$post_type];
    
    		// Remove the last part
    		$post_type_archive_slug = ($do_not_append_slug) ? preg_replace('/(.*)\/([^\/]+)$/', '$1', $post_type_archive_slug) : $post_type_archive_slug;
    
    		// Remove the dynamic parts
    		$post_type_archive_slug = preg_replace('/((?:[^\/]+)?(?:%[^\/]+%)(?:[^\/]+)?)/', '', $post_type_archive_slug);
    
    		// Remove duplicated slashes
    		$post_type_archive_slug = trim(preg_replace('/(\/+)/', '/', $post_type_archive_slug), '/');
    
    		return $post_type_archive_slug;
    	} else {
    		return '';
    	}
    }
    
    function pm_post_type_archive_link($link, $post_type) {
    	$pm_archive_base = pm_get_post_type_archive_base($post_type);
    
    	return ($pm_archive_base) ? sprintf('%s/%s', trim(get_home_url(), '/'), $pm_archive_base) : $link;
    }
    add_filter('post_type_archive_link', 'pm_post_type_archive_link', 99, 2);
    Thread Starter mufimufin

    (@mufimufin)

    I have just changed it and it works, but I am still facing that “old URL still working” issue.

    Thank you for all your help so far.

    Plugin Author Maciej Bis

    (@mbis)

    How about .htaccess redirect? That would be the easiest solution.

    Thread Starter mufimufin

    (@mufimufin)

    I can have a look at it. The thing is that I wanted to get rid of unecessary redirects (redirect hell) on the page. The previous URL is not used at all. This is a new website.

    Plugin Author Maciej Bis

    (@mbis)

    Ok, if that is a new website you do not need to redirect the old ones. The canonical URL (meta tag) for old archive page will use the new correct one. This should prevent Google from indexing the incorrect archive address.

    Thread Starter mufimufin

    (@mufimufin)

    In other words I can ignore the fact that old english URL`s are still responding with content and not 404, because all robots will go to the new URL instead, right?

    Plugin Author Maciej Bis

    (@mbis)

    Yes, exactly. As long as you use canonical URL meta tags:
    https://moz.com/learn/seo/canonicalization

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Permastructures changes are not being reflected on the website’ is closed to new replies.