S
S
Sergey Vinogradov2020-08-26 17:32:07
Nginx
Sergey Vinogradov, 2020-08-26 17:32:07

Autoconversion of htaccess to nginx config doesn't work, why?

Usually, under Apache, I use htaccess for a number of my sites with truncating the file extension (file.*) and redirecting from index.* to a slash (or without), then they switched to nginx/1.14.1 (PHP FastCGI) and I don’t have it turns out to convert the config, I used different online converters, below I post the conf from the most famous one, everything seems to be correct, but either the pruning of htm file extensions does not work, or the server crashes. For me, only lines with address rewriting are important. Where is the mistake?

# htaccess configuration
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} /index.htm
RewriteRule ^(.*)$ http://site.ru/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.htm\ HTTP
RewriteRule ^([^.]+)\.htm$ http://site.ru/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.htm [L]
AddType application/x-httpd-php .html .htm


# nginx configuration

charset utf-8;

location ~ (\.[^./]+)$ {
}

location / {
  rewrite /index.htm http://site.ru/ redirect;
  rewrite ^/([^.]+)\.htm$ http://site.ru/$1 redirect;
  if (!-e $request_filename){
    rewrite ^(.*)$ /$1.htm break;
  }
}

location ~ \.(html|htm)$ {
  if (!-e $document_root$document_uri){return 404;}
  fastcgi_pass localhost:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-08-26
@dodo512

AddType application/x-httpd-php .html .htm

In order for php-fpm to execute code in .htm files, you need to edit security.limit_extensions
if ($request_uri ~ "/index\.htm") {
    return 301 /;
}

if ($request_uri ~ "^([^.?]+)\.htm") {
    return 301 $1;
}

location / {
    try_files $uri $uri/ $uri.htm$is_args$args;
}

location ~ \.htm {
    try_files $uri =404;
    
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question