Apache mod_rewrite on caddyserver?


(Tobias) #1

At the moment I’m trying to serve sendy.co with Caddyserver.
But sendy.co uses the “mod_rewrite” from apache server.
What do you think is it possible to serve sendy.co with Caddyserver?

The problems are:
mod_rewrite is not enabled
display_errors is turned off


(Andrew Steinborn) #2

Could you post the .htaccess file here?


(Tobias) #3

someone wrote for ngix the configuration file:

#Sendy configuration

   location ^~ /newsletters {

           root /home/abc/public_html/;

           error_log /var/log/nginx/sendy.error.log;

           index index.php index.html index.htm;

           try_files $uri $uri/ $uri.php?$args;

           rewrite ^/newsletters/w/([a-zA-Z0-9/]+)$ /newsletters/w.php?i=$1 last;

           rewrite ^/newsletters/l/([a-zA-Z0-9/]+)$ /newsletters/l.php?i=$1 last;

           rewrite ^/newsletters/t/([a-zA-Z0-9/]+)$ /newsletters/t.php?i=$1 last;

           rewrite ^/newsletters/subscribe/([a-zA-Z0-9/]+)$ /newsletters/subscribe.php?i=$1 last;

           rewrite ^/newsletters/unsubscribe/([a-zA-Z0-9/]+)$ /newsletters/unsubscribe.php?i=$1 last;

           location ~ \.php$ {

                   try_files $uri =404;

                   fastcgi_split_path_info ^(.+\.php)(/.+)$;

                   fastcgi_pass php;

                   fastcgi_index index.php;

                   include fastcgi_params;
           }

           location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
                   access_log off;

                   log_not_found off;

                   expires 30d;
           }
   }

(Tobias) #4

and this is the original .htaccess file:

ErrorDocument 404 "[404 error] If you're seeing this error after install, check this FAQ for the fix: https://sendy.co/troubleshooting#404-error"

Options +FollowSymLinks
Options -Multiviews

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-]+)$ $1.php [L]

# Link tracker
RewriteRule ^l/([a-zA-Z0-9/]+)$ l.php?i=$1 [L]

# Open tracker
RewriteRule ^t/([a-zA-Z0-9/]+)$ t.php?i=$1 [L]

# Web version
RewriteRule ^w/([a-zA-Z0-9/]+)$ w.php?i=$1 [L]

# unsubscribe
RewriteRule ^unsubscribe/(.*)$ unsubscribe.php?i=$1 [L]

# subscribe
RewriteRule ^subscribe/(.*)$ subscribe.php?i=$1 [L]

(Andrew Steinborn) #5

You can try this:

rewrite {
    regexp ^/([a-zA-Z0-9-]+)$
    to /{1}.php
    regexp ^/l/([a-zA-Z0-9/]+)$
    to /l.php?i={1}
    regexp ^/t/([a-zA-Z0-9/]+)$
    to /t.php?i={1}
    regexp ^/w/([a-zA-Z0-9/]+)$
    to /w.php?i={1}
    regexp ^/unsubscribe/(.*)$
    to /unsubscribe.php?i={1}
    regexp ^/subscribe/(.*)$
    to /subscribe.php?i={1}
}

I don’t use Sendy but this should work.


(Tobias) #6

thank you very much for this.

Unfortunately there is a 404 error mesage. https://sendy.co/forum/discussion/5/404-error-after-install/p1

Do I have to tell Caddyfile where the location of sendy is? Because it is in a folder of a hugo site.

Do I have to use “rewrite /sendy” in the Caddyfile?

rewrite /sendy {
    regexp ^/([a-zA-Z0-9-]+)$
    to /{1}.php
    regexp ^/l/([a-zA-Z0-9/]+)$
    to /l.php?i={1}
    regexp ^/t/([a-zA-Z0-9/]+)$
    to /t.php?i={1}
    regexp ^/w/([a-zA-Z0-9/]+)$
    to /w.php?i={1}
    regexp ^/unsubscribe/(.*)$
    to /unsubscribe.php?i={1}
    regexp ^/subscribe/(.*)$
    to /subscribe.php?i={1}
}

But I get also a 404 error.


(Tobias) #7

is there also a chance to avoid regexp?
At the moment I’ve not really understand this rewrite process…


(Matthew Fay) #8

Not sure re: 404 errors, but to answer this:

Not with those rewrites. You need the capture groups (in parentheses) so you can extract that information and use it in the rewritten URI (those {1}s for example).


(Andrew Steinborn) #9

Okay, I didn’t know it was in a folder. I went off the nginx configuration you posted.

Try this, assuming it’s in the /sendy folder:

rewrite /sendy {
    regexp ^([a-zA-Z0-9-]+)$
    to {1}.php
    regexp ^l/([a-zA-Z0-9/]+)$
    to l.php?i={1}
    regexp ^t/([a-zA-Z0-9/]+)$
    to t.php?i={1}
    regexp ^w/([a-zA-Z0-9/]+)$
    to w.php?i={1}
    regexp ^unsubscribe/(.*)$
    to unsubscribe.php?i={1}
    regexp ^subscribe/(.*)$
    to subscribe.php?i={1}
}

(Tobias) #10

:sleepy: unfortunately there is still the 404 error.

My Caddyfile looks like:

domain.com {

expires {
 match .css$ 1y
 match .js$ 1m
 match .png$ 1d
 match .jpg$ 1m
 match .pdf$ 1i
 match .txt$ 1s
 match .html$ 5i30s

}
minify
 header /static/ Cache-Control "max-age=2592000"
gzip

root            /var/www/tobias/public       # Tells CADDY which folder t$
basicauth       /admin xxxxxxxxx xxxxxxxxxxxxxxx                         # p$
hugo            /var/www/tobias            # Tells HUGO which folder to e$
fastcgi / /var/run/php/php7.0-fpm.sock php

rewrite /sendy {
    regexp ^([a-zA-Z0-9-]+)$
    to {1}.php
    regexp ^l/([a-zA-Z0-9/]+)$
    to l.php?i={1}
    regexp ^t/([a-zA-Z0-9/]+)$
    to t.php?i={1}
    regexp ^w/([a-zA-Z0-9/]+)$
    to w.php?i={1}
    regexp ^unsubscribe/(.*)$
    to unsubscribe.php?i={1}
    regexp ^subscribe/(.*)$
    to subscribe.php?i={1}
}

}

(Minimalist) #11

Sorry to open an old discussion, but has someone figured this out? Sendy won’t work with Caddy…

The .htaccess from sendy looks like this:

ErrorDocument 404 "[404 error] If you're seeing this error after install, check this FAQ for the fix: 
https://sendy.co/troubleshooting#404-error"

Options +FollowSymLinks
Options -Multiviews

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-]+)$ $1.php [L]

# Link tracker
RewriteRule ^l/([a-zA-Z0-9/]+)$ l.php?i=$1 [L]

# Open tracker
RewriteRule ^t/([a-zA-Z0-9/]+)$ t.php?i=$1 [L]

# Web version
RewriteRule ^w/([a-zA-Z0-9/]+)$ w.php?i=$1 [L]

# unsubscribe
RewriteRule ^unsubscribe/(.*)$ unsubscribe.php?i=$1 [L]

# subscribe
RewriteRule ^subscribe/(.*)$ subscribe.php?i=$1 [L]

I tired the provided solution above from @astei - but for me it is not working.

Thanks everyone!


(Toby Allen) #12

If you are getting 404’s a good way to debug is to add

rewrite_url

or

rewrite_uri 

to your log as it will output what the url is being rewritten to which should help you to diagnose 404’s

log / mylogfile.log "{common} {rewrite_url} {rewrite_uri}"

(Minimalist) #13

Thanks @tobya for trying to help. I added what you proposed. But that doesn’t help me. I get:

184.22.39.190 - - [04/Feb/2018:14:43:47 +0000] "GET 
/unsubscribe/Tr6WUocrY099WIlUCdjpY007639Kb6v7zntTjj8D0cw763w/xJzb7631K892MXYfczFWq5EBoQ/xJzb7631K892MXYfczFWq5EBoQ HTTP/2.0" 404 38 - /unsubscribe/Tr6WUocrY099WIlUCdjpY007639Kb6v7zntTjj8D0cw763w/xJzb7631K892MXYfczFWq5EBoQ/xJzb7631K892MXYfczFWq5EBoQ 

And in the Caddyfile I have:

rewrite {
    r ^unsubscribe/(.*)$
    to unsubscribe.php?i={1}
}

Same for:

184.22.39.190 - - [04/Feb/2018:14:47:36 +0000] "GET /w/xJzb7631K892MXYfczFWq5EBoQ HTTP/2.0" 404 38 - /w/xJzb7631K892MXYfczFWq5EBoQ

I have in the Caddyfile:

rewrite {
    r ^w/([a-zA-Z0-9/]+)$
    to w.php?i={1}
}

Any ideas?!


(Matthew Fay) #14

Looks like they’re not being modified at all. At a guess:

Because URIs always start with a forward slash, and your start-of-line anchor (the ^ symbol) precludes an opening /, those regexes would never match and no rewrites are taking place.

Try add a forward slash just after the start-of-line anchor and test again.

I’d also advise using a site such as https://regexr.com/ to test your regexes before using them.


(Minimalist) #15

Wow thank you @Whitestrake now it works with the following rewrites:

rewrite {
    r ^/([a-zA-Z0-9-]+)$
    to {1}.php
}
rewrite {
    r ^/w/([a-zA-Z0-9/]+)$
    to w.php?i={1}
}
rewrite {
    r ^/l/([a-zA-Z0-9/]+)$
    to l.php?i={1}
}
rewrite {
    r ^/t/([a-zA-Z0-9/]+)$
    to t.php?i={1}
}
rewrite {
    r ^/unsubscribe/(.*)$
    to unsubscribe.php?i={1}
}
rewrite {
   r ^/subscribe/(.*)$
   to subscribe.php?i={1}
} 

Yippie!!