Dynamic routes not working in React with Caddy

I have a React Application. I am using some routes that receives parameters in the URL (by GET), for example 应用宝官网-全网最新最热手机应用游戏下载 that works pretty good in local enviroment but no in my CentOS server. I have to mention that I am using npm run build to generate the static files.

For other side, I have running Caddy as HTTP server in the CentOS server, there I have problem only with the routes that includes parameters, so now am wondering if caddy have something to be with this issue.

Here the React people say:

If you’re using Apache, you need to create a .htaccess file in the public folder that looks like this:

Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]

So, how can I do that in Caddy? how is the equivalent?. I have currently these lines for the site:

mysite.com {
   root PathToProject/build/
   log logs/pagelog.log
}

What is missing in order to can use parameters in the URL?

RewriteCond %{REQUEST_FILENAME} !-f

This part essentially says, “If the request is not for an existing file:”

RewriteRule ^ index.html [QSA,L]

And this part says, “Serve index.html instead”.

Caddy’s rewrite directive can handle this kind of fallback for you. The following example would attempt to serve the requested file, then serve index.html if it can’t:

rewrite {
  to {path} index.html
}

https://caddyserver.com/docs/rewrite

1 Like

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