A
A
Alexey Chertok2019-11-13 18:04:54
Nginx
Alexey Chertok, 2019-11-13 18:04:54

How to compose a regular expression for NGINX?

Hello. Please help me with this regex.

rewrite ^/(ru|en)?/?(faq|news|page|catalogue|basket|photos|search|users)(.*)$ /?lang=$1&module=$2&mod_rewrite=$3 break;

What is the actual problem. It is necessary to truncate any other links like http:/site.com/xxx through nginx. So that when it hits all such links, it sends to 404. I did it. But ran into a problem. Website in 2 languages.
And when, for example, I enter http:/site.com/xxx, it redirects me to 404, and when http:/site.com/en/xxx, it does not redirect me anywhere, except perhaps to the main page.
How to make non-existent pages work through languages ​​(en, ru) too? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-11-14
@dodo512

server {
    ...

    root /home/site/public_html;

    rewrite ^/(ru|en)?/?(faq|news|page|catalogue|basket|photos|search|users)(.*)$ /index.php?lang=$1&module=$2&mod_rewrite=$3;
    rewrite ^/$ /index.php;

    location / { }

    location ~ \.php$ {
        fastcgi_pass unix:/tmp/php-fpm7.sock;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|woff|ogg|mpe?g|avi|zip|gz|rar)$ {
        add_header Pragma public;
        add_header Cache-Control "public";
        access_log /home/site/cache.log;
        expires 1d;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question