Remove Slug From Custom Post Type URL?

On www.webhostingbreak.com I have custom post types for web hosting companies.
Their url is /hosting-directory/ and unfortunately, since then (aug2010), we’ve still got all those pages at PR0 and rankings overall have been terrible.

The only guess I have is that because visiting /hosting-directory/ causes a 404 error, Google might not see pagerank flowing from the root domain to a subpage of hosting-directory.

Read More

We want to remove /hosting-directory/ altogether. We tried doing that but we couldn’t get normal posts in a category to work.

Is there any workaround for this? Custom post types destroyed our rankings and pagerank since august 2010.

Be well and talk soon.

Related posts

Leave a Reply

2 comments

  1. If you want to remove the slug you need to do one of two (2) things:

    1. Stop using custom post types because you don’t seem to understand what they are or are meant to be used for. Use pages instead. They already do EXACTLY what you want to do, permalinks-wise.

    2. Add a custom rewrite rule for each and every individual ‘hosting company’ added in the admin like this:

      add_rewrite_rule('(the-post-slug)/?$','index.php?<post type>=$matches[1]','top');
      

      You should add this to your theme’s functions.php file or in a plugin; I’d suggest a mu-plugin, personally. You could even automate this programmatically, but that would end up costing an extra database query per page load. Just make sure you don’t run those before the 'setup_theme' hook (the rewrite API isn’t initialized before that hook). Hooking on 'init' would be plenty safe.

      Then you would have to flush the rewrite rules by going to /wp-admin/options-permalinks.php (just visiting the page is enough.)

  2. Another way to achieve this is by making your plugin with hooks like:

    add_filter( 'rewrite_rules_array', onFilterRewriteRulesArray ); 
    add_action( 'wp_loaded', onLoadedWP );
    function onLoadedWP()
    {       
        //Must be done once after rules change
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
    
     function onFilterRewriteRulesArray( $rules )
        {
            $newrules = array();
            $newrules['([^/]+)$'] = 'index.php?brandbook=$matches[1]';
            return $newrules + $rules;
        }
    

    Instead of brandbook put your custom post type name/slug.

    You can modify regex to your needs as well.

    With this code page opens without redirect to custom post type with both of these links:

    http://localhost/postname
    http://localhost/brandbook/postname