Feature Proposal: Support in TWiki.pm for Shorter URLs, ommiting HomeWeb and WebHome
Motivation
In
ShorterUrlCookbook (
Changes to lib/TWiki.pm - for extra shortening
) it is hinted that some code could be add to TWiki.pm to give extra short URLs
Description and Documentation
This proposal is to add a config var, $TWiki::cfg{ShortURLs} that if defined will make autolink topics omit the home topic and even the home web, if defined by the $TWiki::cfg{HomeWebName}, as proposed in
CreateHomeWebVariable.
By default TWiki behavior is not changed, but a new
bin/configure variable can be set,
ShortURLs, that, when set, will make html links created from WikiWords use the short URL form as defined in
ShorterUrlCookbook, i.e.
- omitting the topic name if it is the Home Topic (i.e. the
$TWiki::cfg{HomeTopicName} var which defaults to WebHome)
- also omitting the web name if it is the Home web (i.e. the
$TWiki::cfg{HomeWebName} var which defaults to Home)
- Note that omitting the
bin/view prefix is done by setting also the existing TWiki configure var
$TWiki::cfg{ScriptUrlPaths}{view} = '';
in =lib/LocalSite.cfg
This supposes that the installation is set up to expand back the short URLs should be complemented by, for instance in apache config adding these rules: (these ones suppose that twiki is at the root of the web site, and no perl extension are needed)
RewriteEngine on
RewriteRule ^/Home/WebHome$ / [R=permanent,L]
RewriteRule ^/([A-Z].*)/WebHome$ /$1/ [R=permanent,L]
RewriteRule ^/Home/([^/]*)$ /$1 [R=permanent,L]
RewriteRule ^/bin/view/(.*)$ /$1 [R=permanent,L]
RewriteRule ^/bin/view$ / [R=permanent,L]
RewriteRule ^/([A-Z][^/]*/.*[^/])$ /bin/view/$1 [PT]
RewriteRule ^/([A-Z].*)/$ /bin/view/$1/WebHome [PT]
RewriteRule ^/([A-Z][^/][^/]*)$ /bin/view/Home/$1 [PT]
RewriteRule ^/$ /bin/view/Home/WebHome [PT]
Examples
See
http://wiki.koalaz.net/
navigate in it and observe the URL:
Impact
Implementation
See proposal implementation as an attached patch
420-shorturls.diff to TWiki-4.2.0 (apply with
patch -p0 <420-shorturls.diff It modifies
lib/TWiki.pm lib/TWiki.spec
My working dir for this is availabe on the mercurial repository:
http://hg.colas.nahaboo.net/twiki-colas/42-shorturls/
--
Contributors: ColasNahaboo - 27 Jan 2008
Discussion
Colas, I think it would be helpful if you added a declaration of what system paths are assumed in this topic. Specifically, I want to know if your code above assumes that the TWiki installation is in the root or in the typical
/twiki subdir. This helps me understand when the given paths should read e.g.
/bin/view or
/twiki/bin/view. Then I can test this on
TWikiVMDebianStable as well!
--
TorbenGB - 12 Feb 2008
Ah, normally if your twiki is installed in
/twiki, but you want the URLs at root, I think the rules become:
RewriteEngine on
RewriteRule ^/Home/WebHome$ / [R=permanent,L]
RewriteRule ^/([A-Z].*)/WebHome$ /$1/ [R=permanent,L]
RewriteRule ^/Home/([^/]*)$ /$1 [R=permanent,L]
RewriteRule ^/twiki/bin/view/(.*)$ /$1 [R=permanent,L]
RewriteRule ^/twiki/bin/view$ / [R=permanent,L]
RewriteRule ^/([A-Z][^/]*/.*[^/])$ /twiki/bin/view/$1 [PT]
RewriteRule ^/([A-Z].*)/$ /twiki/bin/view/$1/WebHome [PT]
RewriteRule ^/([A-Z][^/][^/]*)$ /twiki/bin/view/Home/$1 [PT]
RewriteRule ^/$ /twiki/bin/view/Home/WebHome [PT]
--
ColasNahaboo - 12 Feb 2008
FWIW, i usually have something like this:
RewriteEngine on RewriteBase / # rewrite /(.*) to /bin/view/$1, except for /bin, /pub # RewriteCond %{REQUEST_URI} !^[/]robots.txt RewriteCond %{REQUEST_URI} !^[/]bin/ RewriteCond %{REQUEST_URI} !^[/]pub/ RewriteRule (.*) /bin/view/$1 [L]
and then set this in
LocalSite.cfg:
$TWiki::cfg{ScriptUrlPaths}{view} = 'http://www.foo.nl';
Of course, that doesn't rid you of the Home/WebHome in generated links...
--
KoenMartens - 15 Feb 2008
Ah, I forgot to add that my config is set up so that lower-case urls still go through unchanged, only urls starting with caps are changed. This way I can have other dirs than thw TWiki ones (local scripts, ...)
--
ColasNahaboo - 15 Feb 2008
Accepted by 14 day rule
--
KennethLavrsen - 28 Feb 2008
This seems to run OK. I will have to fix 2 issues however:
- [1] put a proper entry for ScriptUrlPaths config var, next to the ShortURLs one, or see if this var cannot be removed and be automatically deduced and/or defaulted from ShortURLs
- [2] (fixed, see below) In practice, TWiki webmasters get confused by having to end urls to webs (without a topic) by /. See how we could make the engine "guess right" in this cases (you ask for
/Foo, and make the engine guess that you mean /Foo/WebHome and not /Home/Foo if the topic Home.Foo do not exist).
--
ColasNahaboo - 19 Mar 2008
No offense but is there a more effective way in changing the core than rewrite rules? It's like if you want short urls, you're most likely stuck with apache. I'd understand if the argument against is due to apache's popularity.
Since it's for 5.0, we can do more for the core?
--
KwangErnLiew - 21 Mar 2008
Whoa, long time no action..
- I fixed my second point [2] (now if you omit the trailing / like in .../Foo/Bar TWiki will first check that we are not meaning Foo/Bar/WebHome if it cannot find a topic Bar in a web Foo). See patch in 423-shorturls.diff. I will just have to tackle my point [1] above, and should be ready to commit.
- The URL to my HG repository is now http://hg.colas.nahaboo.net/twiki-colas/42-shorturls/
- To Kwang: I do not think it is possible to have this kind of short URLs without rewrite rules. Note that other engines than apache have them too. And I think we should make the common installation dead simple. But I guess for an webserver-agnostic solution, having a single one letter view script at root may be a solution, e.g.: my.site/v/Foo/Bar
--
ColasNahaboo - 22 Sep 2008
Found a bug:
--
ColasNahaboo - 23 Sep 2008
Interesting work, I'm looking forward to this. Thanks!
--
KeithHelfrich - 24 Sep 2008
Ok, I fixed my 2 last issues:
- [1] now setting ShortURLs from
bin/configure will automatically define $TWiki::cfg{ScriptUrlPaths}{view} = ''; no need to edit lib/LocalSite.cfg by hand anymore
- [3] fixed by a fix in the rules of the RewriteEngine
I will commit to subversion trunk as soon as
CreateHomeWebConfigVar is ready too (both extensions go hand in hand, really)
--
ColasNahaboo - 24 Sep 2008
I am setting this to parked and no committed developer. Please feel free to flip that and own & implement.
--
PeterThoeny - 2010-08-01