Changing RSS feed URL structure

I am in the process of creating a wordpress website, and I just have one issue left – RSS feeds.

Currently, the website has a .htaccess file blocking access to /category/* pages. It was part of a requirement that the site is not obviously WordPress, and /category is a decent indicator of that.

Read More

Each category is fronted by a page, which displays the category content – so /category/news becomes /news.

However, RSS feeds always seem to point to /category/xxx/feed – is there a way to remove this /category/ part? So people could view the RSS feed at http://example.com/news/feed ?

I’ve tried fiddling around with the .htaccess file, but it’s the one area of building a web application I’m not 100% sure on. Every search has returned with results of how to point a feed to Feedburner instead, which is not what I am looking to do at all.

Related posts

3 comments

  1. After some thought – this can be achieved with some creative abuse of existing feeds:

    • easy part – redirect category feeds to page comment feeds
    • hard part – make page comment feeds think they are category feeds

    Something like this:

    Category_Feed_At_Page::on_load();
    
    /**
     * Repurpose page feeds for category of same name feeds.
     */
    class Category_Feed_At_Page {
    
        static function on_load() {
    
            add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
    
            add_action( 'do_feed_rdf', array( __CLASS__, 'do_feed' ), 9 );
            add_action( 'do_feed_rss', array( __CLASS__, 'do_feed' ), 9 );
            add_action( 'do_feed_rss2', array( __CLASS__, 'do_feed' ), 9 );
            add_action( 'do_feed_atom', array( __CLASS__, 'do_feed' ), 9 );
        }
    
        /**
         * Change page's comment feed into category feed.
         *
         * @param WP_Query $query
         */
        static function pre_get_posts( $query ) {
    
            if ( $query->is_main_query() && $query->is_page() && $query->is_feed() ) {
                $name = $query->get( 'pagename' );
    
                require_once( ABSPATH . 'wp-admin/includes/taxonomy.php' );
    
                if ( category_exists( $name ) ) {
                    $category = get_category_by_slug( $name );
    
                    $query->set( 'category_name', $name );
                    $query->set( 'cat', $category->term_id );
                    $query->set( 'pagename', '' );
    
                    $query->is_page         = false;
                    $query->is_comment_feed = false;
                    $query->is_category     = true;
                    $query->is_singular     = false;
    
    
                    remove_action( 'do_feed_rdf', array( __CLASS__, 'do_feed' ), 9 );
                    remove_action( 'do_feed_rss', array( __CLASS__, 'do_feed' ), 9 );
                    remove_action( 'do_feed_rss2', array( __CLASS__, 'do_feed' ), 9 );
                    remove_action( 'do_feed_atom', array( __CLASS__, 'do_feed' ), 9 );
    
                    remove_action( 'template_redirect', 'redirect_canonical' );
                }
            }
        }
    
        /**
         * Redirect real category feed to page feed.
         */
        static function do_feed() {
    
            if ( ! is_category() )
                return;
    
            $name = get_query_var( 'category_name' );
            $page = get_page_by_path( $name );
    
            if ( ! empty( $page ) ) {
                wp_safe_redirect( get_post_comments_feed_link( $page->ID ) );
                die;
            }
        }
    }
    
  2. I found (in my opinion) the best and easiest way to do this, and thought I should leave it as an answer in case anybody stumbles upon this thread.

    I downloaded and installed the WP No Category Base plugin and that did exactly what I needed.

    Unfortunately, I never got around to trying the other answers (had to move on to a more urgent project), so I can’t clarify if any of them work.


    Updated Link: No Category Base (WPML)

  3. You do know that you can change the tag and category slug prefixes on the permalinks settings page, right?

    So if it bothers you to have the “category” in “/category/news”, but accepts having “/cat/news”, you can change that there.

Comments are closed.