Help converting .htaccess over to Caddy redir

I need help converting this .htaccess file over to Caddy redir.

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^.*$ ./index.php

I’m not the best with regex and stuff along the lines of that. Anything helps, thanks!

This is quite a common question. @mholt Perhaps we should have a wiki page for common conversions from Apache or Nginx?

Anyway, what your are looking for would be this…

rewrite {
	to {path} {path}/ /index.php

From the docs, It’s kinda hidden under the status field.

Rewrite will check each destination in order and rewrite to the first destination that exists. Each one is checked as a file or, if ends with /, as a directory. The last destination will act as default if no other destination exists.

The example in the docs is as follows:

rewrite {
    if {file} not favicon.ico
    to {path} {path}/ /maintenance.html /index.php

It’s quite a nice one. The first bit will check to see if the request is for the file favicon.ico and even if it doesnt exist it wont try and rewrite it. Since most browsers will try and request for a favicon.ico file and if it doesn’t exist you dont want to waste time and server resources calling PHP for it. The other bit that’s different is /maintenance.html which means, if it exists Caddy will serve that file instead of calling into your PHP code. This is good for the situation where you are performing updates and and don’t want requests coming through. Instead visitors will be served a static html file that lets them know your working on the site and to come back later :slight_smile:


Thanks so much, I appreciate the explanation and other help too.

There are and will be plenty of converts coming from Apache world.

It might be worthwhile to add .htaccess parser to Caddy and/or ship a converter utility with Caddy. It could cut down on a number of conversion requests.

1 Like

Converter would be better. .htaccess support wouldn’t be good.

Why would it not be good? It may be challenging and may not support everything Apache has…

However, this one feature would simplify life for many users when it comes to deploying webapps as many are shipped with Apache-centric configs. This one feature alone would be a huge boost to Caddy’s marketshare.

Just look how many config conversion requests are there on this forum alone.

1 Like

Guess you’re right. idk.

Nginx doesn’t have .htaccess either. Personally to me, it is not the way to go.

I agree with @abiosoft here. I think it would slow down the web-server for not a lot of gain.

@slightfoot, that’s why you make per-folder/domain overrides optional. Enabled only when you want to. Apache does it exactly this way. Does that alleviate your performance anxiety? :slight_smile:

Having this config overrides feature is huge gain for flexibility of configuration. Just look through the forums, there are multiple request for this.

You could probably sell this for money, if it existed and worked well.

1 Like

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