D
D
Di Lee2019-01-03 21:08:03
Nginx
Di Lee, 2019-01-03 21:08:03

Nginx: How to rewrite all root requests to index.php?

Where is the error in the nginx config? It is necessary to make sure that when calling any file from the root of the site (even if it does not exist), there will
be a redirect to /index.php without replacing the URL in the browser
.

location ~ /[^\/]+ { 
    rewrite  ^/[\w\W]+$  /index.php;
  }

or
location ~ /[^\/]+ { 
    rewrite  ^/[^\/]+$  /index.php;
  }

Works in a particular case
location = /robots.txt {
   	rewrite ^/robots.txt$ /robots.txt.php;
  }

Here, when a specific file is called (robots.txt), another one is called (robots.txt.php), without redirection.
But you need to make sure that when you call any file, there is also a redirect without a redirect to index.php

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin B., 2019-01-03
@denlem

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

B
Boris Syomov, 2019-01-03
@kotomyava

Something like this:

location = /index.php {
  # тут проксирование или fcgi
}

location ~ /[^\/]+ { 
  rewrite .* /index.php;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question