Rewrite a url based on folder path


(Maisnam Raju) #1

Hi, I am trying to convert the following nginx code

 location / {
      auth_basic "Restricted Content";
      auth_basic_user_file /var/www/.htpasswd;
      if (!-e $request_filename){
          rewrite ^(/)?customer/.*$ /customer/index.php;
      }

      if (!-e $request_filename){
          rewrite ^(/)?backend/.*$ /backend/index.php;
      }

      if (!-e $request_filename){
          rewrite ^(.*)$ /index.php;
      }
      index  index.html index.htm index.php;
      # try_files $uri $uri/ /index.php$is_args$args;
  }

to a caddy equivalent.
Here is the .htaccess code for it

#php_value default_charset UTF-8
#AddDefaultCharset UTF-8 
#make sure nothing can be seen inside!
deny from all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

I tried doing

{
    root /var/www
    fastcgi / php-fpm:9000 php {
        index index.php
    }

    # To handle .html extensions with laravel change ext to
    # ext / .html

    rewrite ^(/)?customer/.*$ /customer/index.php
    rewrite ^(/)?backend/.*$ /backend/index.php
    rewrite ^(.*)$ /index.php


    gzip
    browse
    log /var/log/caddy/access.log
    errors /var/log/caddy/error.log
    # Uncomment to enable TLS (HTTPS)
    # Change the first list to listen on port 443 when enabling TLS
    tls mymail@gmail.com

    # To use Lets encrpt tls with a DNS provider uncomment these
    # lines and change the provider as required
    #tls {
    #  dns cloudflare
    #}
}

But It brought up a lot of errors for them.


(Matthew Fay) #2

The nginx code looks awfully difficult to parse, but the htaccess seems much simpler. It looks like it’s simply running through these checks:

  1. Is it a file? Serve the request normally. Else…
  2. Is it a directory? Serve the request normally. Else…
  3. Serve the root index (and append the URI as a query).

Something like this would be equivalent:

rewrite {
  to {path} {path}/ /?{uri}
}

(Maisnam Raju) #3

Hi, I have tried the following so far

{
    root /var/www
    fastcgi / php-fpm:9000 php {
        index index.php
    }

    # To handle .html extensions with laravel change ext to
    # ext / .html
   
    rewrite {
        regexp ^/(.*)$
        to {path} {path} /index.php?q={1}
    }

    rewrite {
        regexp ^/(.*)?cliente/.*$
        to {path} {path} /customer/index.php?q={1}
    }

    rewrite {
        regexp ^/(.*)?backend/.*$
        to {path} {path} /backend/index.php?q={1}
    }

but i get a 400 error since its not able to find the /backend and the /cliente routes. I have also also tried using the same config with
rewrite /routename {
}

But no luck so far.


(Matthew Fay) #4

It might be because you’re rewriting to {path} {path} instead of to {path} {path}/ in each rewrite? That could cause it to miss certain directory requests. Might also be because of regex misses.

The static file server will issue canonical index redirects if it ever gets a chance to serve an index file. The best way to refer to a directory index is using the directory itself, not the index file.

Also, you can ditch the regex, they’re slow. Placeholders are much faster.

rewrite {
  to {path} {path}/ /?q={uri}
}

rewrite /cliente {
  to {path} {path}/ /cliente/?q={uri}
}

rewrite /backend {
  to {path} {path}/ /backend/?q={uri}
}