Apache mod_rewrite to Caddyfile

I was able to install and configure Caddy with Phalcon3 crazy easily, however, to actually be able to use Phalcon correctly I need to translate a mod_rewrite block to Caddy speak.

The block is;

<IfModule mod_rewrite.c>
    <Directory "/var/www/test">
        RewriteEngine on
        RewriteRule  ^$ public/    [L]
        RewriteRule  ((?s).*) public/$1 [L]
    </Directory>
    <Directory "/var/www/test/public">
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
    </Directory>
</IfModule>

My current Caddyfile is;

:4401 {
        root /var/www
        gzip
        fastcgi / /run/php/php7.0-fpm.sock php
}

I appreciate any help.

@zqueal, Phalcon needs a .so to works, no? Phalcon Framework is a module to NGINX and/or Apache - PHP 5.6 and 7.0… I do not know if works properly with PHP 7.1. Anyway, you need to use Caddy as proxy server first and running Phalcon Framework into NGINX or Apache.

Phalcon needs a .so to works, no?

Yes. It has one–and it’s loaded into PHP7 just fine;

root@na01:~# locate phalcon.so
/usr/lib/php/20151012/phalcon.so

oWPPw

From what I can tell it’s working just fine.

Oh! Nice! Loaded as PHP module, not a NGINX or Apache module. Sorry for mistake about who load this module. More rewrite details, check Caddy Docs.

Caddy

rewrite {
    r .*
    ext /
    to {path} {path}/ /index.php?_url=/{path}&{query}
    # below, an alternative, maybe
    # to {path} {path}/ /index.php?_url=/{path}&{uri_escaped}
}
1 Like

Yup, I was blown away at how fast and easy it was to setup and get working, even though Phalcon mentions Caddy zero times in the documentation.

Thanks for the rewrite rules! I’ll give them a shot and then include them in the Phalcon database if I can.

EDIT:

Unfortunately, those rules don’t seem to work. I can directly access Phalcon by using the full uri value;

http://hostname.ext/index.php?_url=/

Using both, / works fine, but /anything does not and returns HTTP/500.

The last thing I tried before I gave up for today was;

:4401 {
    root /var/www
    gzip
    fastcgi / /run/php/php7.0-fpm.sock php
    rewrite {
            r .*
            ext /
            to {path} {path}/ /index.php?_url=/{uri}
    }
}

After trying to {path} {path}/ /index.php?_url=/{path}&{uri_escaped} Caddy refused to start under any circumstances no matter what I did–even after reverting the changes in my Caddyfile.

As far as I can tell the string should be to {path} {path}/ /index.php?_url=/{uri} but it simply doesn’t work.

1 Like

Drop r subdirective. Selecting via regex is unnecessary, and a waste of resources just to match everything (which rewrite does by default, unless you specify a basepath).

Drop ext subdirective. That would cause your rewrite not to work unless the URL has no file extension. It’s not necessary because the first rewrite target is {path}.

Escaped URI is not necessary. Placeholder {uri} includes leading slash, so prepending another is not necessary.

rewrite {
	to {path} {path}/ /index.php?_url={uri}
}
2 Likes

Trailing slashes are unecessary? {uri} instead {path}&{query} or {query}? I’m asking because I really do not know the boundaries between one and the other - in practice.

Trailing slashes are at your discretion, but I try to avoid worrying about them.

The leading slash is unnecessary, though.

For a request for /folder?foo=bar#anchor:

{uri}      = /folder?foo=bar#anchor

{path}     = /folder

{query}    = foo=bar

{fragment} = #anchor

https://caddyserver.com/docs/placeholders

2 Likes

Awesome!

Thanks for all your help fellas. That seems to have done it just fine!

1 Like

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