Rewrite possible for url with parameters?

In the past I published a link http://wiki.passchier.net/index.php?page=Thai+Bible+Translations to a wiki page on a different system. Nowadays, this page is served on wiki.passchier.net through the markdown plugin on Caddy.

I would like to keep the link working by using rewrite but haven’t succeeded – perhaps because the url parameter syntax gets in the way? It just returns a 404.
Also just creating a softlink on the filesystem didn’t work.

Solved it without rewrite:

  • made index.php softlink to index.md
  • added ext .php to the markdown directive
1 Like

If there are other/better solutions, I’d love to hear, but it’s working for me now like this.

To clarify, you had a page at http://wiki.passchier.net/index.php?page=Thai+Bible+Translations and you now want to rewrite requests for this address to a page you’re currently serving via markdown?

What’s the address of the page you want to serve, and what does your Caddyfile look like right now?

The address now is wiki.passchier.net/thaibible.md and I have a softlink pointing to it at index.md so just wiki.passchier.net serves the page.
The Caddyfile:
wiki.passchier.net {
root /var/www/caddywiki/
log stdout
errors stderr
minify
gzip
ext .md
markdown {
ext .php
css /wiki.css
js /wiki.js
}
}
(The ext .php I added to solve this problem, together with a softlink index.php to thaibible.md)

Option 1:

  • Remove markdown ext .php subdirective
  • Rename index.php symbolic link to index.md
    (Should “just work” as you already have an ext .md directive, so Caddy should try index.md first)

Option 2:

  • Remove markdown ext .php subdirective
  • Unlink index.php
  • Rewrite requests for the base directory to return the specific file you want
  • Your Caddyfile would look like this:
wiki.passchier.net {
    root /var/www/caddywiki/
    log stdout
    errors stderr
    minify
    gzip
    ext .md
    rewrite / /thaibible.md
    markdown {
        css /wiki.css
        js /wiki.js
    }
}

Thanks for your help Matthew. Even though I got it working (see above), I wanted to try your suggestions.

Option 1:
The wiki works, but the redirect link 404s.
(Really need index.php to be tried, and option 1 removes that totally.)

Option 2:
Same, the wiki works, but the redirect link 404s.
(Again, no reference to index.php, so why would it get picked up by markdown?)

When you say “Really needs index.php to be tried”, do you mean that you want it to try index.php for some specific reason? Or do you mean that ext .md isn’t working like I thought?

What dictates the requirement for a reference to index.php? Perhaps I am misunderstanding the function of ext, but with that rewrite, when Caddy gets a request for /, it pretends it was actually a request for /thaibible.md and serves that markdown file instead of any index files at site root. You’ve specified a markdown directive, which by default will process .md files. There should be no need whatsoever for any index files with this method, regardless of extension.

Does thaibible.md actually exist in the site root?

My problem was that I want this link to be serving the wiki page:

It asks for index,php so if it isn’t referenced, the above link won’t lead anywhere.
For the rest the page at http://wiki.passchier.net worked fine as it was.

I had written the page in thaibible.md but then realized I needed index.md. I could have renamed thaibible.md, but instead I made a softlink index.md pointing to it for future extension.

Ahhh! I understand. My bad.

wiki.passchier.net {
    root /var/www/caddywiki/
    log stdout
    errors stderr
    minify
    gzip
    ext .md
    # Serve thaibible.md at wiki.passchier.net:
    rewrite / /thaibible.md
    # Serve thaibible.md at wiki.passchier.net/index.php?page=Thai+Bible+Translations:
    rewrite /index.php?page=Thai+Bible+Translations /thaibible.md
    markdown {
        css /wiki.css
        js /wiki.js
    }
}

This is what I tried initially. The error log says:
[ERROR 500 /index.php] open /var/www/caddywiki/index.php: no such file or directory
Which made me realize I need to make reference to index.php and ignore the other information.

But, this rewrite works: rewrite /index.php /thaibible.md !
It’s probably going to be slightly slower than the filesystem solution, but it will be the Caddy way of solving it.

Why will it be slower? (I must have missed something.)

I’m not sure why rewrite /index.php?page=Thai+Bible+Translations would fail, is that expected behaviour or a bug of some kind?

You could potentially try a workaround, for the sake of specificity:

rewrite /index.php {
    if {query} ends_with Thai+Bible+Translations
    to /thaibible.md
}

I don’t believe that rewrite /index.php will be measurably slower than a filesystem solution. Think of it as instead of Caddy doing a rewrite, your filesystem is doing a rewrite. My workaround might be a hair’s breadth behind, because of the substring match, but there’s no regex so it should be quite servicable even with a very high workload.

After having thought about it, Caddy would know how URLs work, and to only look for the file part. I’m happy for index.php?anything to be redirected, so I won’t get more restrictive, but that line is a good one. I wonder how far back you can go. Would if {query} ends_with ?page=Thai+Bible+Translations also work? That would be most specific.

I guess it will actually be faster, because it doesn’t require an extra filesystem access. Thanks for setting me right!

1 Like

It would not work, because {query} does not include the preceding question mark. The absolutely specific version would be if {query} is page=Thai+Bible+Translations, but I would test that before deploying to be certain.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.