homepage Welcome to WebmasterWorld Guest from 54.225.24.227
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Content Management
Forum Library, Charter, Moderators: ergophobe

Content Management Forum

    
How to customize Wordpress' category url
jimmychuang




msg:3825881
 5:38 am on Jan 14, 2009 (gmt 0)

How to change this: http://example.com/category/category-name/

TO

http://example.com/category-name/

[edited by: ergophobe at 4:17 pm (utc) on Jan. 14, 2009]
[edit reason] No URLs and no keywords allowed [/edit]

 

ergophobe




msg:3826214
 4:22 pm on Jan 14, 2009 (gmt 0)

I'm not sure you can. You can change it to something else, but I don't think you can get rid of it and I don't think you should - what if you name a post "category-name"? How then to you get to your category archive?

Look on the permalink settings page. That's where you change it.

If you're looking to use that second option for inbound links or something, you could always use rewrites.

reprint




msg:3826354
 7:10 pm on Jan 14, 2009 (gmt 0)

Here is the reference in the WP codex
[codex.wordpress.org...]
The various tags are shown there.

you can also do a .htaccess rewrite but know what you are doing and test it carefully.

Things may break depending on how you structure your urls e.g. Calendar function may require dates

Like Ergophobe, I dont see why you would have /category/category-name/ as it seems redundant. Perhaps you want /category/postname/ ?

ergophobe




msg:3826461
 9:46 pm on Jan 14, 2009 (gmt 0)

reprint, I edited the keywords out of Jimmy's post. It wouldn't be redundant as the urls would look like this

example.com/category/widgets
example.com/category/whozits

So it's not so much the redundancy there, but the namespace collision of you have a page (or depending on your permalink structure even a post) at

example.com/widgets

and then you go and tag something "widgets". Of course you can avoid that by always adding something numeric, but it just seems that forcing that structure on WP seems like a bad idea. You could reduce the "categories root" to a single letter (example.com/c/widgets) but I would hesitate to get rid of it entirely.

reprint




msg:3826706
 3:21 am on Jan 15, 2009 (gmt 0)

Ergophobe, ok, I am following now and agree with you. As you rightly point out, over-simplifying urls will likely cause collisions

jimmychuang




msg:3832289
 3:30 pm on Jan 22, 2009 (gmt 0)

Hi, I guess I can't remove category

I have checked some blogs, they are the same with mine.

[edited by: ergophobe at 5:48 pm (utc) on Jan. 22, 2009]
[edit reason] removed keywords [/edit]

Murasaki




msg:3848174
 6:18 pm on Feb 12, 2009 (gmt 0)

Putting the collision issue aside, the change is doable through Wordpress using add_filter and the rewrite_rules_array hook.

Add the following codes in your functions.php

function custom_rewrite_rules($rules) {

// The default rewrite rules are stored inside $rules which is part of the $wp_rewrite object.
// The array keys are the pattern terms and the values are the substitution terms.
$custom_rules = array();
$custom_rules['(.+?)/feed/(feed¦rdf¦rss¦rss2¦atom)/?$'] = 'index.php?category_name=$matches&feed=$matches[2]';
$custom_rules['(.+?)/(feed¦rdf¦rss¦rss2¦atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$custom_rules['(.+?)/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$custom_rules['(.+?)/?$'] = 'index.php?category_name=$matches[1]';

// You might want to remove the default rules otherwise the original default permalink URLs still works.
unset($rules['category/(.+?)/feed/(feed¦rdf¦rss¦rss2¦atom)/?$']);
unset($rules['category/(.+?)/(feed¦rdf¦rss¦rss2¦atom)/?$']);
unset($rules['category/(.+?)/page/?([0-9]{1,})/?$']);
unset($rules['category/(.+?)/?$']);

return $custom_rules + $rules;
}
add_filter('rewrite_rules_array','custom_rewrite_rules');

Note:
If you apply new rewrite rules using the rewrite_rules_array filter you need to refresh the rewrite rules that Wordpress has stored in a hidden option by resaving the Permalink Structure in the admin panel.

[1][edited by: Murasaki at 6:54 pm (utc) on Feb. 12, 2009]

ergophobe




msg:3848199
 6:43 pm on Feb 12, 2009 (gmt 0)

Thanks Murasaki.

So what do you think of the namespace issue? Just take care of it by adding a numeric post_id on the end of every post URL? Or recipe for trouble?

Murasaki




msg:3848222
 7:25 pm on Feb 12, 2009 (gmt 0)

I wouldn't do what jimmy is trying to do. Changing http://example.com/category/category-name/ to http://example.com/category-name/ is not going to work. The rewrite rule pattern for http://example.com/category-name/ is something like ^(.+?)/?$ which matches with anything. It needs something unique/static or a special pattern to separate it from the other pages and in this case, the '/category/' part was it.

ergophobe




msg:3848226
 7:32 pm on Feb 12, 2009 (gmt 0)

Thanks.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Content Management
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved