Rewrite all requests to index.php with nginx

nginx rewrite
nginx redirect all requests to maintenance page
nginx rewrite not working
nginx redirect cheat sheet
nginx not redirecting to index php
nginx redirect to another domain
nginx try_files
nginx location

In my apache configuration I have the following simple rewrite rule which

  1. unless file exists will rewrite to index.php
  2. on the urls you never see the file extension (.php)

how can I rewrite this in nginx?

#
# Redirect all to index.php
#
RewriteEngine On

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]

Here's how my nginx server block looks like now, but it doesn't work :(

root /home/user/www;
index index.php;

# Make site accessible from http://localhost/
server_name some-domain.dev;


###############################################################
# exclude /favicon.ico from logs
location = /favicon.ico {
    log_not_found off;
    access_log off;
}   

##############################################################
# Disable logging for robots.txt
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}   

##############################################################
# Deny all attempts to access hidden files such as 
# .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}   

##############################################################
#   
location / { 
    include /etc/nginx/fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME  $document_root/index.php$args;
    fastcgi_pass    127.0.0.1:9000;
}   

###############################################################
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
    access_log off;
    expires    30d;
}   

###############################################################
# redirect server error pages to the static page /50x.html
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root html;
}   

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#   
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    # With php5-cgi alone:
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

1 unless file exists will rewrite to index.php

Add the following to your location ~ \.php$

try_files = $uri @missing;

this will first try to serve the file and if it's not found it will move to the @missing part. so also add the following to your config (outside the location block), this will redirect to your index page

location @missing {
    rewrite ^ $scheme://$host/index.php permanent;
}

2 on the urls you never see the file extension (.php)

to remove the php extension read the following: http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/

and the example configuration from the link:

location / {
    set $page_to_view "/index.php";
    try_files $uri $uri/ @rewrites;
    root   /var/www/site;
    index  index.php index.html index.htm;
}

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/site$page_to_view;
}

# rewrites
location @rewrites {
    if ($uri ~* ^/([a-z]+)$) {
        set $page_to_view "/$1.php";
        rewrite ^/([a-z]+)$ /$1.php last;
    }
}

Nginx - Route all requests to single script, Add this, location / { try_files $uri $uri/ /index.php; }. What it does is it first check the existence of $uri and $uri/ as real files/folders and if they don't exist will just go  In my apache configuration I have the following simple rewrite rule which unless file exists will rewrite to index.php on the urls you never see the file extension (.php) how can I rewrite this in

Perfect solution I have tried it and succeed to get my index page when I have append this code in my site configuration file.

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

In configuration file itself explained that at "First attempt to serve request as file, then as directory, then fall back to index.html in my case it is index.php as I am providing page through php code.

Nginx fails to redirect '/' to '/index.php', Nginx fails to call index.php file at '/' endpoint. /.well-known { allow all; } location /phpmyadmin { index index.php; } # Rewrite rules for request: “GET / HTTP/2.0”, upstream: “fastcgi://127.0.0.1:9000”, host: “pinkbulletin.com”. The rewritten URL uses two NGINX variables to capture and replicate values from the original request URL: $scheme is the protocol ( http or https) and $request_uri is the full URI including arguments. For a code in the 3xx series, the url parameter defines the new (rewritten) URL. return (301 | 302 | 303 | 307) url;

To pass get variables as well use $args:

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

How to Create NGINX Rewrite Rules, Rewrite rules change part or all of the URL in a client request, usually for NGINX returns a redirect to /index.php and passes the query‑string  Redirect All Requests To Index.php Using .htaccess. In one of my pet projects, I redirect all requests to index.php, which then decides what to do with it: Simple Example. This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:

Flat and simple config without rewrite, can work in some cases:

location / {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /home/webuser/site/index.php;
}

Convert Apache Rewrite Rules to NGINX Rewrite Rules, Learn how to convert Apache HTTP server rewrite rules to NGINX rewrite rules that matches the rewritten URL (and all other requests to www.example.org). On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond  With this, all requests go to that index.php file for handling provided there’s no file or directory already. It works pretty well and IMHO it easier to maintain than Apache2. It works pretty well and IMHO it easier to maintain than Apache2.

Using nginx $is_args instead of ? For GET query Strings

location / { try_files $uri $uri/ /index.php$is_args$args; }

nginx redirect all requests to index.html, Hi All, I'm working on Vuejs SPA + Lalavel API + Laradock latest. I'm trying to setup domain for SPA myapp.dev also I need to change nginx  Rewrite all requests to index.php with nginx; IIS URL Rewrite Module : Redirect Based On QueryString; Hide GET parameter from URL; How to check whether mod_rewrite is enable on server? Url rewrite does not install on windows 10 IIS 10; How to hide the .html extension with Apache mod_rewrite; Can I use commas in a URL? RewriteCond to exclude a

Get Nginx to always serve index, In Nginx this is achieved by rewrite <regexp-pattern> <target-url> command in There is inexhaustible supply of guides on PHP with Nginx  We are using NGINX on our cPanel server to reverse proxy ZKTeco ZKBioSecurity servers. Due to compatibility with some of their devices not supporting HTTPS, all our servers use HTTP, but, of course, all sessions to our NGINX server is secured with HTTPS and a Sectigo certificate provided by cPanel’s AutoSSL.

Rewrite all requests to AltoRouter, Nginx nginx.conf. try_files $uri /index.php;. All requests are now handled by the same file. In that file, you create an AltoRouter instance. $router = new  Using AltoRouter Rewrite all requests to AltoRouter To use AltoRouter, you will need to rewrite all requests to a single file in which you create an instance of the AltoRouter class. There are various ways to go about this, but here are examples for Apache and Nginx.

How to Convert .htaccess Rules to NGINX Directives, The NGINX rewrite directives will also need to be placed within the server block. Many server RewriteRule ^index.php$ - [L] RewriteCond 

Comments
  • Unfortunately it didn't really work. I get an empty response from nginx. In error log it reports the following 2012/10/17 15:23:14 [alert] 17437#0: worker process 17440 exited on signal 11 (core dumped)
  • the combination of the two configs probably messes things up when you combine them like this, do they work when you only try one at a time?
  • can u tell me the configuration for nested levels also. this configuration works well on first level routes
  • This method is less versatile than the higher rated answers here. Particularly you'll get into problems using this method behind a reverse proxy where the url will be rewritten to the local hostname and not the public one.
  • Wow, that is way too easy.
  • This works but get variables aren't passed correctly anymore. Any ideas on how to do that?
  • @Ben I believe this will do the trick: try_files $uri $uri/ /index.php?$args;
  • You're my hero!
  • @Ben try: try_files $uri $uri/ /index.php$is_args$args;
  • Warning: this only works for urls that does not end with .php , for example it doesn't work with /example.php , example.php will NOT be redirected to index.php
  • Good one! Just what I looking for, works for me and I think that it's an optimum solution, faster than rewriting.
  • @Lix I agree, this is a better solution. If it needed anything else at all it would be worth the effort on overhead. iutinvg, +1 for fast static route.
  • The Nginx documentation advises against proxying all calls to PHP: wiki.nginx.org/Pitfalls#Proxy_Everything
  • NGINX advises against only in the case that you have static content to serve as well. If this is not your case, I believe this is the best solution.
  • **Warning: this only works for urls that does not end with .php , for example it doesn't work with /example.php , example.php here will NOT be redirected to index.php **