29 November 2004

TxP: Clean Category URLs the lazy way

I wanted clean urls, with minimal work. OH how to do it ….

Clean urls are so much nicer, and I believe better spidered. Getting clean category urls the easy way (aka. the lazy way)

My site nav is hard coded into a custom form, so I changed the urls to what I want.

Which is better:

/technology/?c=css
or
/technology/c/css

Same amount of characters, but clean urls looks nicer! So how to do this with minimal effort. First I changed my custom form with the new urls. Next some ReWrite, which I was nervous being completely beginner at it

##clean category
RewriteEngine On
RewriteRule ^([A-Za-z0-9_]+)/c/([A-Za-z0-9_+]+)[/]*$ $1/?c=$2    

Basically transforms new url to old form. Advantages are that it doesn’t mess up anything the way TextPattern does things. No custom hacking to make upgrade harder.

extra credit

I am also using my one plugin (because I discovered them a little later) [stw\_category_tree](http://forum.textpattern.com/viewtopic.php?id=2252 “Plugin: stw_category_tree”) to output categories for internal section nav. Thanks again santheo. So I need to change the output of that too.

<txp:stw_category_tree start="technology" onclass="li_curr" prune="true" />
[about line 63-64 change to this]    
$path = ($url_mode==1) ? $pfr.$sec."/" : $path."s=".$sec."&";
return tag($cat,'a',' href="'.$path.'c/'.urlencode($cat).'"');

I could of changed to accept a paramter clean=“true”, but I am the only one using this version.

Anyway, voila clean category urls the easy way.

updated 11/29: Added a “+” sign to ReWrite rule to cover Category names with spaces

updated 11/30: Added missing line from altering stw\_category_tree

 

Comment

what they saidwho said it
If you have the time - please make a ‘how-to-for-dummies’! 2004-12-01
dibbuk
Daniel,

I tripped on over here from the Textpattern forum. If this little “clean URLs” fix of yours is as simple as it seems, you will make a lot of people happy, myself included. However, like dibbuk indicated above, you need to rewrite your instructions for someone with a complete lack of knowledge for PHP. Also, here’s some other things I would consider in a rewrite:

1) Number the steps of the process so it’s clear where a person should be along the way.

2) Indicate the name and location of the files you are suggesting needs edited. For example, you mention some Rewrite needing done…OK…what file(s) do you edit, and where is that file(s) located? The point is, be very specific, don’t assume anyone knows anything.

3) You have some grammar problems in your text, you might want to revise that. For example, you wrote, “I could of changed to accept…” This could be rewritten as “I could have changed this to accept…”

Actually, I’m not sure what the relevance of that entire sentence in #3 above is anyway, which brings up my final suggestion…

4) Be concise. Don’t say what you don’t need to say, or what doesn’t have any relevance to the intructions. Also, keep in mind that this post is going to be long-term, right? So if you say something like, “but I am the only one using this version.” then how are people going to interpret that when your trick becomes more popular with others. Keep it simple and relevant.

I don’t mean to sound nit-picky, but if you can clarify what you’ve done here, and where, me and many others might be using your efforts, and giving you much praise for it.

Destry
2004-12-03
Destry
OK, spent a little more time looking around your pad, and I need to say two things:

First, I saw your cover statement about grammar skills. You’re off the hook…I guess.

Second, nice looking site design. For someone who claims “no design skills,” you did a darn good job.

-D
2004-12-04
Destry
Thanks Destry .. I am a hack in all aspects, but especially design. My grammar doesn’t have to be bad… I am just too lazy, I guess.

I will attempt to rewrite to somewhat simplier instructions.
2004-12-05
DannyB


note: you can only submit after you hit preview


nuff-respec is a weblog written by daniel bulli a senior web programmer in boston, ma.
more >

contact | resume | profile | twitter

recently :::

diversions :::

45+ Amazing Insect Shots in Photography
Insects are one of the most fascinating creatures on earth. There are more than 800, 000 species of insects in the world.
Grayscale color | Stroep Blog
This is how I create a grayscale color in actionscript 3.
Google Flash API
This is great ... google has made this easy ... stay tuned to see what i am working on ...
25 Free Mac Apps That Will Boost Your Productivity
There are many applications that can help you work faster and efficiently. Though, not many applications come cheap.
you still want more »