Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Remove "/categories" from permalink?

Hello, is there a way to change the permalink so that category pages can simply show without the "/categories" in the permalink?
IE. "www.forum.com/lounge" rather than "www.forum.com/categories/lounge"

I think this format is much simpler and the SEO isn't really a significant issue at this point (not as important as "/discussion/#/threadtitle").
Thank you!

Comments

  • R_JR_J Ex-Fanboy Munich Admin

    It wouldn't be that easy. And what if someone decides to have a category called "discussions"? That would screw up the syntax.

  • @R_J said:
    It wouldn't be that easy. And what if someone decides to have a category called "discussions"? That would screw up the syntax.

    True. It would also mess up links like /profile and /messages if they were in some case to be made categories as well. I'm still kind of curious on how to do so but of course will take precaution when managing categories and avoid conflicting URLs.

  • R_JR_J Ex-Fanboy Munich Admin

    This might help you: http://vanillaforums.org/discussion/28400/how-to-change-category-slug-url
    I think there is no real answer to your question but at least it is a start.

  • byakkobyakko New
    edited September 2015

    Thanks R_J

    I managed to set the URL to www.forum.com/(categoryname) using the code below which changes all the links but the individual category pages still remain attached to www.forum.com/categories/(categoryname)

    <?php if (!defined('APPLICATION')) exit(); function CategoryUrl($Category, $Page = '', $WithDomain = TRUE) { $Category = (object)$Category; //$Result = '/'.$Category->CategoryTitle.'/'.Gdn_Format::Url($Category->Name); $Result = '/'.Gdn_Format::Url($Category->Name); //no slug return Url($Result, $WithDomain); }

    I've been trying to find something in application/vanilla that would allow me to actually remove "/categories" but still no luck. :(

  • byakkobyakko New
    edited September 2015

    Update: I tried adding a route with these settings:

    Route: ^(.*)
    Target: categories/$1
    Type: Internal

    Which DID successfully manage to change www.forum.com/categories/(categoryname) to www.forum.com/(categoryname)!

    BUT the dashboard turns into a white page and the routing messes up other things.

    Any thoughts?

    I'm currently looking into the shortprofileUrls plugin (http://vanillaforums.org/addon/shortprofileurls-plugin) to see how they managed to do this. Not sure if it will be the same for categories though.

  • byakkobyakko New
    edited September 2015

    The error I receive when I set it to (.), ^(.), ^(.*)?$, etc. (I honestly have no clue how routes work so I'm just trial-and-error-ing.)

    Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) in /home/byakko/public_html/vanilla/library/core/class.dispatcher.php on line 632

    Again, using this does manage to change the URL to my liking to www.forum/(categoryname) but it breaks the dashboard and other pages.

  • hgtonighthgtonight ∞ · New Moderator

    @byakko said:

    I'm currently looking into the shortprofileUrls plugin (http://vanillaforums.org/addon/shortprofileurls-plugin) to see how they managed to do this. Not sure if it will be the same for categories though.

    This is a good idea.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • I GOT IT!!!

    All I had to do was manually rout each category name. Didn't know it was that easy. All this time I was trying to use /categories/$1.

    Target: /categories/name
    Route: name
    Type: Internal

    Just do this for all of your categories and also remember to add bootstrap.after.php to your /conf redefining CategoryUrl() and you're all set :pleased:

  • x00x00 MVP
    edited September 2015

    @byakko it is possible to dynamically route with a hook, something like (not tested):

        protected function dynamicRoute(&$routes, $route, $destination, $type = 'Internal', $oneway = FALSE) {
            $key = str_replace('_', '/', base64_encode($route));
            $routes[$key] = array($destination, $type);
            if ($oneway && $type == 'Internal'){
                if (strtolower(Gdn::request()->path()) && strpos(strtolower($destination), strtolower(Gdn::request()->path()))===0){
                    Gdn::dispatcher()->dispatch('Default404');
                    exit;
                }
            }
        }
    
        public function base_beforeLoadRoutes_handler($sender, &$args) {
            $categoryModel = new CategoryModel();
            $categories = $categoryModel->getFull();
    
            foreach($categories As $cat) {
                $this->dynamicRoute($args['Routes'], '^' . $cat->UrlCode, '/categories/' . $cat->UrlCode, 'Internal', true);
            }
        }
    

    You could even add the CategoryUrl function after the plugin class.

    grep is your friend.

  • The previous code I posted forgot page navigation, here's the correct one

    <?php if (!defined('APPLICATION')) exit(); function CategoryUrl($Category, $Page = '', $WithDomain = TRUE) { $Category = (object)$Category; //$Result = '/'.$Category->CategoryTitle.'/'.Gdn_Format::Url($Category->Name); $Result = '/'.Gdn_Format::Url($Category->Name); //no slug if ($Page) { if ($Page > 1 || Gdn::Session()->UserID) $Result .= '/p'.$Page; } return Url($Result, $WithDomain); }

    • Correct routes are:

      Route: name(.*)
      Target: categories/name$1

Sign In or Register to comment.