SID-02333: changing the links to attachments to dynamic
| Status: |
Asked |
TWiki version: |
6.0.2 |
Perl version: |
|
| Category: |
CategoryAttachments |
Server OS: |
ubuntu 14.04 |
Last update: |
8 years ago |
Hi, firstly I apologise for logging in as
TWikiGuest - I tired to register but got an error message that this had failed and email couldn't be sent to my address. But that's not what I'm asking about here.
I have a very successful instance of TWiki which is much used and liked, so many thanks to all involved. Following feedback about the user's experience I would like to either change the static links on attachments or, via mod_rewrite, trap them in apache and redirect to a cgi script.
This cgi script, which is working fine, converts some common file types which the browser can't render into types it can, such as pdf, and then sends that converted file to a dynamically created iframe in the browser. The idea is that it will be more user-friendly than making them download files and open them in order to find that they weren't quite what one was after, or just to get a quick glance at something.
The attachments can be accessed internally at
/var/www/twiki/pub//MyWeb/MyTopic/MyAttachment
or from outside by a GET of the form
https://aaa.bbb.ccc/twiki/pub/MyWeb/MyTopic/MyAttachment
What I want to do is trap this and replace it with
https://aaa.bbb.ccc/cgi-bin/MyFileViewer.sh?topic=MyTopic&attachment=MyAttachment
The problem I have is that as soon as I put
RewriteEngine On (with or without the subsequent
RewriteRule) in either twiki.conf, or in a specially created .htaccess file in /pub/MyWeb, then not only does it not actually do the rewrite but it breaks many of the links within Twiki.
So my questions are:
1. Is there a much better way to change the static links to attachments (so I shouldn't bother with apache rewrite), or
2. How can I make a
RewriteRule, and where should I put it, to make these links dynamic.
Thanks in advance
James Collier
--
TWiki Guest - 2017-09-28
Discussion and Answer
This is more an Apache issue than a TWiki issue... my Apache knowledge is getting a bit rusty, but I'll try anyway.
- I am surprised that just turning on the engine (without any rules) breaks TWiki links. I am sure that mod_rewrite can achieve what you want, but this won't help if it breaks the rest of your TWiki. If my following suggestion doesn't work, it may be necessary to turn on mod_rewrite logging (which in Apache 2.4 works quite different than for older version - see http://httpd.apache.org/docs/current/mod/mod_rewrite.html
).
So, instead of
mod_rewrite I'd just use
mod_alias (which, as far as I know, is enabled by default): Your twiki.conf has a line like
Alias /twiki/pub "/var/www/twiki/pub". Replace this line with the following block:
<Location "/twiki/pub/">
RedirectMatch /twiki/pub/(.*?)/(.*?)/(.*) /cgi-bin/MyFileViewer.sh?web=$1&topic=$2&attachment=$3
</Location>
The
Location block will restrict the redirection to URLs starting with
/twiki/pub, and
RedirectMatch picks the three components for web, topic, and attachment and insert them into the appropriate parameters.
A few additional notes:
- Sorry that registration didn't work. I can't help directly, but will pass that info on.
- I just guessed that you might need a "web" parameter in addition to "topic" and "attachment".
- If you use hierarchical webs, then this block doesn't work as it is - you'll need a more complex regular expression.
- As an alternative, you could pass the whole path as one parameter to your viewer script and do the path parsing inside your script. If you replace the query parameter by
PATH_INFO, then a plain Alias directive will do the trick, at the price of a more complex script.
- And, of course, the directory where
/cgi-bin/ sits in, must be enabled with ScriptAlias.
--
Harald Jörg - 2017-09-28
Hi Harald,
I can't begin to say how grateful I am to you - your suggestion works just fine. I'm yet to test it fully, but at the first few 'smoke' tests it looks perfect.
Thank you again,
James Collier
--
TWiki Guest - 2017-09-28
If you answer a question - or someone answered one of your questions - please remember to edit the page and set the status to answered. The status selector is below the edit box.