S
S
Sergey2018-06-21 23:00:02
Nginx
Sergey, 2018-06-21 23:00:02

How to migrate .htaccess rules to Nginx?

Good luck to everyone, I decided to switch to nginx, but I can’t figure out how to rewrite the htaccess rules correctly. Help someone in the subject please, below the htaccess rules:

RewriteEngine On

RewriteBase /

# Protect hidden files from being viewed
<Files .*>
  Order Deny,Allow
  Deny From All
</Files>

# Redirect to www
RewriteCond %{REQUEST_URI} !robots\.txt$
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule .* http://www.%{HTTP_HOST}/$0 [R=301,L]


# Redirect urls with no trailing slash
RewriteCond %{REQUEST_FILENAME} !-f		
RewriteCond %{REQUEST_FILENAME} !-d		
RewriteCond %{REQUEST_URI} !^/odminka/
RewriteCond %{REQUEST_URI} !^/guide
RewriteCond %{REQUEST_URI} !\.xml$
RewriteCond %{REQUEST_URI} !\.txt$

RewriteCond %{REQUEST_URI} !/$
RewriteRule .+ $0/ [R=301,L]


# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

AddDefaultCharset utf-8

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2018-06-22
@kondorkm

If someone faces the same problem, then the solution is below

server {  
    server_name test.ru;
    rewrite ^ http://www.test.ru$request_uri? permanent;
}

server {
    listen 80;
    server_name www.test.ru;
    root /sites/test_ru/public/www;
    index index.php;

    location ~* \.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|ico|css)$ {
        expires max;
    }

    location / { 
  expires off;
  try_files $uri $uri/ @kohana;
    }

    location ~* \.php$ {
        try_files $uri $uri/ @kohana;
  fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; 
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fastcgi_param SCRIPT_FILENAME /path/to/fastcgi_script_name;
        include fastcgi_params;
    }

    location @kohana {
  fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; 
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        include fastcgi_params;
    }

}

M
Mikhail Shatilov, 2018-06-21
@iproger

There is an easy way for you:
1. Install nginx.
2. Create a domain config from the default config.
3. Google each comment from the config for Apache and insert the found version for nginx into the domain file.
Make sure frame/cms/etc supports nginx.
Ps adminka pleased.

S
Sergey Savostin, 2018-06-21
@savostin

something like this

V
Vladimir Mukovoz, 2018-06-22
@castomi

server {
  listen 80;
  listen 443 ssl http2;
  ssl_certificate /cert.crt;
  ssl_certificate_key /key.key;
  server_name www.example.com;
  return 301 https://example.com$request_uri;
}
server {
  listen 80;
  listen 443 ssl http2;
  ssl_certificate /cert.crt;
  ssl_certificate_key /key.key;
  server_name example.com;
  index index.html index.htm index.php index.cgi index.pl index.xhtml;
  location ~ /\. { internal; }
  location / { rewrite ^([^.\?]*[^/])$ $1/ permanent; }
  location /odminka/ { }
  location /guide { }
  try_files $uri $uri/ /index.php/$uri;
  location ~ \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
  }
}

This is of course minimal, but it should smell. Of course, my virtual hosts are much fatter, but it should be appropriate. If required, I can help make a more individual virtual host.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question