.htaccess to caddy v2

1. Caddy version (caddy version):

Caddy version v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

Caddy runs as a web server on port 80 and 443

a. System environment:

Ubuntu 18

b. Command:

sudo systemctl reload caddy 

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

sand.online

root * /var/www
encode gzip
php_fastcgi unix//run/php/php7.4-fpm.sock
file_server
log {
        output file /var/log/caddy/access.log
        format console
    }

3. The problem I’m having:

Please help:
On the domain sand.online/ I want to host different web applications I am testing. The domain/ server will have applications in their own folder e.g for wordpress it is /var/www/wordpress for laravel application /var/www/laravel.

Now I have a customised vanilla php that was working perfect with .htaccess and Apache. The htaccess redirect configurations is:

Options +SymLinksIfOwnerMatch -Indexes
Options -MultiViews

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)(/?)$ index.php?com=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+).htm(l?)$ index.php?com=$1 [L,QSA]

RewriteRule ^([0-9-]+)/([a-zA-Z0-9-]+)(/?)$ index.php?item=$1&com2=$2 [L,QSA]
RewriteRule ^([0-9-]+)/([a-zA-Z0-9-]+).htm(l?)$ index.php?item=$1&com2=$2 [L,QSA]

RewriteRule ^(resource)/([a-zA-Z0-9-]+)(/?)$ index.php?com=$1&dc=$2 [L,QSA]
RewriteRule ^(resource)/([a-zA-Z0-9-]+).htm(l?)$ index.php?com=$1&dc=$2 [L,QSA]

How do I convert the rewrites to Caddy file v2?

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

It would probably be something like this:

@rule1 path_regexp rule1 ^/([a-zA-Z0-9-]+)(\.html|\.htm|/)?$
rewrite @rule1 /index.php?com={re.rule1.1}

@rule2 path_regexp rule2 ^/([0-9-]+)/([a-zA-Z0-9-]+)(\.html|\.htm|/)?$
rewrite @rule2 /index.php?item={re.rule2.1}&com2={re.rule2.2}

@rule3 path_regexp rule3 ^/(resource)/([a-zA-Z0-9-]+)(\.html|\.htm|/)?$
rewrite @rule3 /index.php?com={re.rule3.1}&dc={re.rule3.2}

Just a guess. You might need to play around with the regexes to get it to work exactly the same way, just took a guess here. I combined each of the pairs because I think the (/?) and .htm(l?) parts can be combined to avoid essentially duplicate regexes.

You can rename the rules to whatever makes sense to you, as it’s unclear what the purpose of them are to me from looking at just that config.

2 Likes

Thank you for your prompt reply and thank you it worked.

Follow up
The Caddyfile I have stated:
sand.online
root * /var/www

This domain will have all sought of demo web application. Thus in this case this application (lets call it “myOldPhpApp”) is in the folder /var/www/myOldPhpApp. When I change the Caddyfile to below it works:

sand.online
root * /var/www/myOldPhpApp

It works but the rewrites affects the other applications like the laravel app in this folder e.g /var/www/laravel which I could access it https://sand.online/laravel and wordpress in this folder /var/www/wordpress url https://sand.online/wordpress. The laravel and wordpress is not working after I implement the rewrite code. But when I return to the previous configuration and remove the rewrites the wordpress and laravel works but oldPhp does not.

Thus my question is, how do I implement the rewrite to only implement to /var/www/myOldPhpApp folder? That is https://sand.online/oldPhpApp will have the rewrites but https://sand.online/laravel will not have the rewrites.
Before with Apache I just used the .htaccess in the folder to control.

Thank you once again.

I think this wiki article should answer your questions:

Thank you! The wiki did help.

1 Like