harrisonr
(Harrison R)
December 18, 2021, 6:48pm
1
Hello, does anyone know what htaccess rules these would be in caddy?
`
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+).php - [L,R=404]
`
Please fill out the help topic template. Itâs very hard to know how to help without proper context for your question.
What version of Caddy are you running? Whatâs your Caddyfile config? What have you tried? Whatâs in your logs? What are you trying to do exactly?
By the fact I see .php
in what you wrote though, I can only assume youâre looking for the php_fastcgi
directive to serve a PHP app.
1 Like
harrisonr
(Harrison R)
December 21, 2021, 7:41pm
3
Iâm using caddy v2.
I have a PHP application and created my rewrite rules which work fine. However, Iâm trying to block direct access to the file.
For example I have a file called organisation-list.php and the rewrite simply rewrites to domain.net/organisations
But, I want to stop people being able to access that page via domain.net/organisation-list.php (a 404 if possible).
Is this possible via caddy?
harrisonr
(Harrison R)
December 21, 2021, 8:50pm
4
I got this working with the following:
rewrite /organisation-list.php /404.php
rewrite /organisations /organisation-list.php
matt
(Matt Holt)
December 21, 2021, 9:02pm
5
That will work, but keep in mind that the server will still reply with a 200 status instead of a 404, which is probably your intent for the first one. Instead you might want error /organization-list.php 404
.
harrisonr
(Harrison R)
December 21, 2021, 9:46pm
6
Thanks, just did that.
Iâm trying to rewrite my URL from domain.net/profile.php?id= &action= to domain.net/profile/ /
So for example, domain.net/profile.php?id=1&action=edit to domain.net/profile/1/edit but the rewrite documentation doesnât seem to cover how to do that (or does it?)
I tried the below but I get this error: watcher unable to load latest config {âconfig_fileâ: âCaddyfileâ, âerrorâ: âadapting config using caddyfile: parsing caddyfile tokens for ârewriteâ: Caddyfile:35 - Error during parsing: Wrong argument count or unexpected line ending after ârewriteââ}
rewrite {
r ^/profile/([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)$
to /profile.php?id={1}&action={2}
}
Thatâs Caddy v1 syntax.
Now, you need to use request matchers, specifically the path_regexp
matcher, and use it along the rewrite
handler.
harrisonr
(Harrison R)
December 21, 2021, 10:39pm
8
Thanks, tried the below but get watcher unable to load latest config {"config_file": "Caddyfile", "error": "adapting config using caddyfile: Caddyfile:36: unrecognized directive: path_regexp"}
path_regexp profile /profile/([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)$
rewrite /profile/{1}/{2} /profile.php?id={re.profile.1}&action={re.profile.2}
I donât know if any of this is right.
Please review the request matching docs, specifically the Syntax section. You need to use a named matcher.
harrisonr
(Harrison R)
December 21, 2021, 10:47pm
10
@profile {
path_regexp profile ^/profile/([-\w]+)/([-\w]+)$
}
rewrite @profile /profile.php?id={re.profile.1}&action={re.profile.2}
This worked, thanks! Can you confirm this is the correct way and best practice?
1 Like
harrisonr
(Harrison R)
December 21, 2021, 10:53pm
11
Is there any way to have error /*.php 404
except my rewrite rules so .php files cannot be accessed directly?
Looks good. You can save a couple lines by using the single-line named matcher syntax:
@profile path_regexp profile ^/profile/([-\w]+)/([-\w]+)$
rewrite @profile /profile.php?id={re.profile.1}&action={re.profile.2}
You can use named matchers with error
as well.
1 Like
harrisonr
(Harrison R)
December 21, 2021, 11:05pm
13
Thanks, any idea why no 404 is showing even if I enter a random link? It just shows my index.php.
https:// {
tls {
on_demand
}
root * /var/www/html
file_server
php_fastcgi unix//run/php/php7.4-fpm.sock
handle_errors {
@404 {
expression {http.error.status_code} == 404
}
rewrite @404 /404.php
file_server
}
}
1 Like
Thatâs how php_fastcgi
works â for any request that doesnât map to a file, itâll rewrite to index.php
.
See this section in the docs for an in depth explanation of how it works:
If you need that requests to unknown files trigger a 404, then you can use the try_files
option of php_fastcgi
to do so. Thereâs an example for that at the bottom of that page in the docs.
1 Like
harrisonr
(Harrison R)
December 21, 2021, 11:19pm
15
Alright, so in theory I could just show a 404 error for everything except my rewrites which is what I want.
https:// {
tls {
on_demand
}
root * /var/www/html
file_server
php_fastcgi unix//run/php/php7.4-fpm.sock
error /*.php 404
@profile {
path_regexp profile ^/profile/([-\w]+)/([-\w]+)$
}
rewrite @profile /profile.php?id={re.profile.1}&action={re.profile.2}
}
Now if I try this, everything shows a 404 including my rewrite?
Did you see the example at the bottom of the php_fastcgi
docs? Iâm pretty sure thatâs exactly what youâre looking for, with =404
in the try_files
harrisonr
(Harrison R)
December 22, 2021, 12:06am
17
When I try
php_fastcgi unix//run/php/php7.4-fpm.sock {
try_files {path} {path}/index.php =404
}
I get 2021/12/22 00:06:10.404 ERROR watcher unable to load latest config {"config_file": "Caddyfile", "error": "adapting config using caddyfile: parsing caddyfile tokens for 'php_fastcgi': Caddyfile:31 - Error during parsing: unrecognized subdirective try_files"}
You never filled out the help topic template. What version of Caddy are you using? Please make sure youâre on the latest, v2.4.6.
harrisonr
(Harrison R)
December 22, 2021, 12:22am
19
I was on v2.4.5 but .6 works now.
I have the below in my config, is there any way to exclude my rewrites to the 404?
error /*.php 404
@profile {
path_regexp profile ^/profile/([-\w]+)/([-\w]+)$
}
rewrite @profile /profile.php?id={re.profile.1}&action={re.profile.2}
You can probably just remove error /*.php 404
, or change the matcher to exclude /profile.php
from matching.