Convert .htaccess caddy rewrite Please help me

RewriteRule ^uploaded/(\w+)/?$ uploaded.php?filecode=$1
RewriteRule ^error/(\w+)/?$ error.php?error=$1
RewriteRule ^download/(\w+)/?$ download.php?filecode=$1
RewriteRule ^check-stats/?$ check-stats.php
RewriteRule ^stats/(\w+)/?$ stats.php?statscode=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* error/404/ [L]

Hi,
I tried this way, but did not succeed :frowning:

rewrite {
r ^uploaded/(\w+)/?$
to uploaded.php?filecode={1}
}

Looks alright at a glance, can you tell us more about your problem? Do you get errors, or does the rewrite simply never happen?

Also, consider using a basepath in your rewrite blocks, as Caddy will check this first and avoid unnecessary regex (a big resource drain). Like the following untested example:

rewrite /uploaded {
    r ^uploaded/(\w+)/?$
    to uploaded.php?filecode={1}
}

Had a further look into this, my advice was a little off re: basepath, as using the basepath removes this from the regex string. Instead, I have successfully tested with this Caddyfile:

:2015 {

    rewrite /uploaded {
        r ^/(\w+)/?
        to /uploaded.php?filecode={1}
    }

    fastcgi / 127.0.0.1:9000 php
    startup php-fpm

    log stdout
    errors stdout

}

The following uploaded.php:
<?php echo '<pre>'; print_r($_GET); echo '</pre>'; ?>

The browser query http://localhost:2015/uploaded/testword is logged as:
172.17.0.1 - [12/Dec/2016:04:48:25 +0000] "GET /uploaded.php?filecode=testword HTTP/1.1" 200 49

Achieving the following result:

Array
(
    [filecode] => testword
)

Took some testing to figure all this out. I imagine it couldn’t hurt to have some more examples of working regex rewrites in the docs, I suppose.

2 Likes

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