How to redirect all Laravel routes to index.php in Nginx
In Laravel framework all the routes should be redirected to index.php and of not the urls will throw 404 error.
We can handle this thought htaccess rules if the web server is Apache.
Since Nginx not supporting htaccess file, the required rules should be configured in virtualhost configuration itself.
The below given rule redirect all Laravel routes to index.php.
try_files $uri $uri/ /index.php?$query_string;
The complete virtualhost configuration is sharing for reference.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Please note that here its using php-fpm for serving the php pages.
That's all…