• Resolved anthonyt24

    (@anthonyt24)


    I have a website with at least 3 categories. I have set one of them as the default category.

    Next step was that I need to have the category slug of the default category to be hidden from the URL slug, and the rest of the categories should be left untouched.

    My current permalink structure is: /%category%/blog/%post-name%/
    example: /en/blog/wordpress-post-sample/

    and I need it to be:
    /blog/wordpress-post-sample/

    and the other categories should remain the same:
    /fr/french-post-title/
    /de/german-post-title/

    My current htaccess contents are as follows:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.website\.com$
    #RewriteRule ^blog/(.*)/?$ /en/blog/$1/ [L]

    I have tried the above but when I visit /blog/wordpress-post-sample/, I get a 404 response.

    Note: If I add R=301 tag, it correctly redirects the url back to /en/blog/ URL but no luck when R=301 is removed.

    PS: I’d like to have a no-plugin solution.

    Appreciate your help. Cheers

Viewing 1 replies (of 1 total)
  • Thread Starter anthonyt24

    (@anthonyt24)

    I solved my problem. All I needed was the WordPress Rewrite API.

    if( ! function_exists('theme_add_rewrite_rules') ){
        function theme_add_rewrite_rules(){
            add_rewrite_rule(
                '^blog/([^/]+)/?$',
                'index.php?name=$matches[1]',
                'top'
            );
        }
    }
    add_action( 'init', 'theme_add_rewrite_rules');

    This solves how wordpress will parse the URL. Half of the problem is how to modify the posts so that the links will not include the /en/ slug. The code below should solve that problem.

    if( ! function_exists('rewrite_blog_url')){
        function rewrite_blog_url($url){
            if( strpos( $url, '/en/blog/' ) >= 0 ){
                return str_replace( '/en/blog/', '/blog/', $url );
            }
            return $url;
        }
    }
    if( ! function_exists('rewrite_blog_post_url')){
        function rewrite_blog_post_url( $url, $post, $leavename ) {
        if ( $post->post_type == 'post' ) {
            return rewrite_blog_url($url);
        }
        return $url;
    }
    }
    add_filter('the_permalink', 'rewrite_blog_url');
    add_filter('the_content', 'rewrite_blog_url');
    add_filter('post_link', 'rewrite_blog_url');

Viewing 1 replies (of 1 total)
  • The topic ‘Hide category name using mod_rewrite’ is closed to new replies.