Nginx rewrite rule for cms backend

I need to make url rewrite rules (server blocks) on the nginx server in the same way as on my previous Apache server.
This is the code from .htaccess that I need to implement (convert) to my existing one:

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ admin/index.php?hotelname=$1&do=$2 [QSA]

RewriteRule ^(([A-Za-z0-9-/]+)+)$ admin/index.php?hotelname=$1 [L]

This code is on my website because I need to hide the folder in the address bar (/ admin /), where the files are located after logging in. And when someone is already logged in, the address bar is similar to www.domain.com/username , and when you click on the menu address like www.domain.com/username/page1 , www.domain.com/username/page2 , www.domain.com/username/page3 . This is what I need to achieve in nginx. Because now a full backend without function. When I enter the backend, I am redirected to www.domain.com/username , but on the screen I see only the file was not found. The backend only works when I manually add www.domain.com/admin/index.php .

This is my actual configuration for nginx:

server_names_hash_bucket_size  64;

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

server {
        listen 80;
        root /usr/share/nginx/www;
        index index.php;
        server_name www.example.com;
        error_page 404 http://www.example.com/404.php;
        autoindex off;



   location / {
        rewrite ^([^\.]*)$ /$1.php;
    }

    location = / {
        rewrite ^ /index.php;
    }


    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    location ~ /\.ht {
            deny all;
      }
}

When I try to change my block to:

location / {
        rewrite ^([^\.]*)$ /$1.php;
        rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ /admin/index.php?hotelname=$1&do=$2;
        rewrite ^/(([A-Za-z0-9-/]+)+)$ /admin/index.php?hotelname=$1 break;
    }

Each of my css file has a 500 error ...

I would be very grateful for any help! Many thanks.

+3
source share
1 answer

You put this in your / location, this means that all your requests do not match before you go here. You must create a location role specific to this entry before the location /

location ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ {
    rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ /admin/index.php?hotelname=$1&do=$2;
} 
0
source

All Articles