Welcome to WebmasterWorld Guest from 54.166.178.177

Forum Moderators: ergophobe

Message Too Old, No Replies

How to customize Wordpress' category url

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

5+ Year Member



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]

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

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

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

5+ Year Member



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/ ?

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

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

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

5+ Year Member



Ergophobe, ok, I am following now and agree with you. As you rightly point out, over-simplifying urls will likely cause collisions
3:30 pm on Jan 22, 2009 (gmt 0)

5+ Year Member



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]

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

5+ Year Member



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]

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

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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?

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

5+ Year Member



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.
7:32 pm on Feb 12, 2009 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Thanks.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month