0

I have many permalinks across my site that looks like this:

http://www.example.com/market/category/slug-here/

and would like to hide /category/ for all permalinks that contain it, like so:

http://www.example.com/market/slug-here/

I tried to use the following rule but did not find success. Anyone know why?

add_rewrite_rule('market/(.*)', 'market/$matches[1]', 'top');

I've verified that the htaccess is being added. No redirects occur though.

note:

I've moved on from this project. As others confirm answers work I'll look into selecting the best answer.

2

I believe the rewrite rule your wanting to alter is this

category/(.+?)/?$ index.php?category_name=$matches[1] category

to do what you want it should just be a case of adding the following rewrite rule in your functions.php

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

this should work but might conflict with other rules so that will be worth checking.

1

If you are looking to get this done super quickly, I would say just use the WordPress SEO Plugin by Yoast. You can find the option to disable the category base under 'Permalinks' in the options panel.

I took a screen shot but I can't show it to you b/c I just started using this site. Anyway, the option is right at the top of 'Permalinks' if you do decide to use that plugin.

1

Add the following to your .htaccess file in between the <IfModule mod_rewrite.c> tags that were created by WordPress:

RewriteCond %{HTTP_HOST}
RewriteRule ^market/(.*)/(.*)$ market/$2 [R=301]

Your .htaccess should looks like the following if it hasn't been modified by another plugin:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# Custom Rewrite
RewriteCond %{HTTP_HOST}
RewriteRule ^market/(.*)/(.*)$ market/$2 [R=301]
</IfModule>
# END WordPress

As a result it will do the following:

http://www.example.com/market/category/slug-here

Redirects to:

http://www.example.com/market/slug-here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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