0

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

https://wordpress.org/support/topic/hide-category-name-using-mod_rewrite

3
  • Usually, if you need a category named "blog", what you really need is to work with different post types.
    – cybmeta
    Jan 7, 2016 at 11:50
  • Hi @cybmeta, I don't have a category name "blog". It is only hard-coded in the permalinks. If we're sticking with this approach, do you perhaps know an htaccess solution? thank you for your response! Jan 7, 2016 at 12:14
  • Sorry, I didn't understand your question correctly. Anyway, I think that using categories to group posts by language is also not good approach. Note that multi-lingual content is not only translate the content, it has a much more things to consider, for example HTML language attribute. You should go with a multi-lingual plugin to handle a multi-lingual site, I think it is your best option.
    – cybmeta
    Jan 7, 2016 at 12:29

1 Answer 1

0

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.