Coverting .htaccess to caddyfile config

1. Caddy version (caddy version):


2. How I run Caddy:

Caddy runs as a web server on port 80 and 443

a. System environment:

Ubuntu 20

b. Command:

paste command here

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:


# Inheritated config
(common) {
    encode zstd gzip
    php_fastcgi unix//run/php/php7.4-fpm.sock

(rewriteDevSiteRule) {
try_files {path} {path}/ /index.*?{query}
@rule1 path_regexp rule1 ^/([a-zA-Z0-9-]+)?$
rewrite @rule1 /index.php?com={re.rule1.1}

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

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

@rule4 path_regexp rule4 ^/(organizations)/([a-zA-Z0-9-]+)?$
rewrite @rule4 /index.php?com={re.rule4.1}&rso={re.rule4.2}&filter={re.rule4.3}

@rule5 path_regexp rule5 ^/(counties)/([a-zA-Z0-9-]+)?$
rewrite @rule5 /index.php?com={re.rule5.1}&county={re.rule5.2}

@rule6 path_regexp rule6 ^/(categories)/([a-zA-Z0-9-]+)?$
        rewrite @rule6 /index.php?com={re.rule6.1}&rsc={re.rule6.2}

@rule7 path_regexp rule7 ^/(counties)/(page={query}&com={query}&county={query})?$
rewrite @rule7 /index.php?com={re.rule7.1}&page={re.rule7.2}&county={re.rule7.3}

@rule8 path_regexp rule8 ^/(resource)/([a-zA-Z0-9-]+)?$
rewrite @rule8 /index.php?com={re.rule8.1}&dc={re.rule8.2}
} {
    root * /var/www/devhub
    import common
    import rewriteDevSiteRule
    log {
      output file /var/log/caddy/devhub.log
      format single_field common_log

3. The problem I’m having:

I am trying to covert this .htaccess config

Options +SymLinksIfOwnerMatch -Indexes
Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?com=$1 [L,QSA]

RewriteRule ^([a-zA-Z0-9-]+)(/?)$ index.php?com=$1 [L,QSA]
RewriteRule ^([0-9-]+)/([a-zA-Z0-9-]+)(/?)$ index.php?item=$1&com2=$2 [L,QSA]
RewriteRule ^(resource)/([a-zA-Z0-9-]+)(/?)$ index.php?com=$1&dc=$2 [L,QSA]
RewriteRule ^(organizations)/([a-zA-Z0-9-]+)(/?)$ index.php?com=$1&rso=$2 [L,QSA]
RewriteRule ^(counties)/([a-zA-Z0-9-]+)(/?)$ index.php?com=$1&county=$2 [L,QSA]

4. Error messages and/or full log output:

The link is the link to the pagination page which is NOT working but County Resources - Devolution Hub link (which is a category) is working.

5. What I already tried:

I have tried the caddyfile config above

6. Links to relevant resources:

1 Like

Just curious, what happens if you remove try_files {path} {path}/ /index.*?{query} from your rewrites snippet?

FYI this segment of try_files will never match, because try_files does lookups for files on disk exactly – it doesn’t use globs for files. You’ll need to use two separate terms for index.php and index.html (which are assume are the two you may care about), choosing the one you prefer first in case there are both.

1 Like

Oh I think I see what’s missing – rewrites will override the existing query portion unless you actually include the existing query. So you’ll need to do stuff like this:

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

i.e. add {query}& at the start of each query in your rewrites.

1 Like

And this one doesn’t quite make sense, because you’re using the {query} placeholder multiple times here in the regexp.

1 Like

Off-topic, but reminds me of an old adage.

A developer has a problem.

They decide they can solve it with regex.

Now the developer has two problems.


Thank you for your prompt reply. This is the problem I am facing:

  1. For this link County Resources - Devolution Hub I used which works:

    @rule5 path_regexp rule5 ^/(counties)/([a-zA-Z0-9-]+)?$
    rewrite @rule5 /index.php?com={re.rule5.1}&county={re.rule5.2}

Now for this link
2. County Resources - Devolution Hub it is not working when I try this

@rule7 path_regexp rule7 ^/(counties)/([a-zA-Z0-9-]+)?$

rewrite @rule7 /index.php?{query}&com={re.rule7.1}&page={re.rule7.2}&county={re.rule7.3}

I even tried putting query in the regexp like this

@rule7 path_regexp rule7 ^/(counties)/{query}?$

rewrite @rule7 /index.php?{query}&com={re.rule7.1}&page={re.rule7.2}&county={re.rule7.3}

Please use triple backticks, i.e. ``` on the lines before and after your config snippets to use code formatting, and use single backticks ` around inline text like URLs. It will prevent the forums software from messing with them.

You’re using three capture groups in your rewrite, but you only have two in your regexp.

This won’t work, you shouldn’t use {query} in the regexp. It doesn’t make sense.

If you need to match on the query containing a certain thing, then you should use the query matcher, something like this:

@rule7 {
	path_regexp rule7 ^/(counties)/([a-zA-Z0-9-]+)?$
	query foo=*

But I don’t think I understand what you’re trying to do.

To be clear, path_regexp only matches the path portion of the URL, not both the path and query.

This topic was automatically closed after 30 days. New replies are no longer allowed.