borjacastillo
borjacastillo

Reputation: 269

Wordpress rewriting URLs

I'm trying to make a Wordpress rewrite rule and it is the first time I do this. I have got some results, but not the ones that I expected.

In my website, I have a blog with many categories (beauty, travelling, etc.). The URL without rewriting is the next one:

http://mywebsite.com/blog/?cat=10 //where 10 is the category of Beauty.

I want it like this:

http://mywebsite.com/blog/beauty

This is the rule I wrote:

add_rewrite_rule('blog/?$?', 'index.php?cat=$matches[1]', 'top');

With this rule, it gets translated to:

http://mywebsite.com/category/beauty/

I guess I'm really close to the solution but I don't know how to get it.

Thank you so much for your support.

Upvotes: 2

Views: 102

Answers (1)

Alexander Flenniken
Alexander Flenniken

Reputation: 432

You generally shouldn't do custom URL rewiting in Wordpress unless you do it the Wordpress way. Wordpress usually reroutes all requests to index.php and then loads the proper content based on the url. Custom rules might interupt this process and cause no content to load at all.

The categories, by default, should appear like http://mywebsite.com/blog/category/{{your-slug}}/. If you want the categories to appear without the /category/ in from of them, you could look into one of the various SEO plugins like YOAST that support this feature. You should be able to use any category slug text you want simply by changing the field in posts > categories.

EDIT

In my previous answer I assumed Wordpress was installed at http://mywebsite.com/blog/. If the Wordpress site url is http://mywebsite.com/, you can use parent-child categories to accomplish a url like this: http://mywebsite.com/category/blog/{{your-slug}}/. Make blog a category and the desired category a child category of blog.

In order to remove the leading category you would use a custom permalink structure in settings > permalinks:

Set Custom Structure: /%postname%/

Set Category base: .

Upvotes: 1

Related Questions