Search engine friendly urls with CherryPy’s default method
My fishing directory haven’t been a success at the search engines and I discovered that most of the pages were listed as Supplemental Results
in Google. One reason for that can be urls with question marks and multiple parameters. So i tried to figure out how to implement that. Apache’s mod_rewrite is a common method for this but
it’s just very cryptic. I implemented a search engine friendly url system for an earlier version of CherryPy (0.x) with a filter but filters just have to be more complicated than the solution I found.
CherryPy has a default method that will be called in case of a partial match. It is explained in the CherryPy tutorial (scroll down to “Partial matches and the default method”). So, just define a method named default with a suitable amount of parameters and expose it. With a few lines of Python code I could transform urls like DOMAIN/?node1=10&node2=20 into DOMAIN/10/20 and DOMAIN/website?wid=123 into DOMAIN/website/123,
def default(self, node1=0, node2=0, pagenr=1, perpage=10):
if node1 == "website":
return self.website(node2)
else: #index method
if node1 == 0 or node1== "0":
node1 = ""
if node2 == 0 or node2 == "0":
node2 = ""
return self.index(node1, node2, pagenr, perpage)
default.exposed = True
The zero-checking is there so I can use DOMAIN/0/10 etc. for urls where node1 isn’t specified and still keep compatibility with the current index method.
I choose to keep perpage and pagenr as normal query string parameters to keep it simple and not overwhelm search engines with a massive amount of pages with friendly urls. The parameter list is adapted at my index method so it is a little hackish to use it for the website method, but it’s simple and it works. Now I just have to change all the output methods and ideally something for 301 redirecting the old-style urls to the new-style urls to get the old urls out of the search engine as fast as possible.