Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx conf how to remove leading slash from $uri

Tags:

nginx

My Nginx conf file :

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

  ## PHP conf in case it's relevant 
  location ~ \.php$ {
  fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  fastcgi_split_path_info ^(.+\.php)(/.*)$;
  include /etc/nginx/fastcgi.conf;
  fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  }

Trying the following URL : http://example.org/login :

expected behavior :

http://example.org/index.php?url=login

actual behavior :

http://example.org/index.php?url=/login

like image 868
kursus Avatar asked Jan 28 '17 10:01

kursus


1 Answers

Use a named location and an internal rewrite. For example:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^/(.*)$ /index.php?url=$1 last;
}

See this document for more.

like image 83
Richard Smith Avatar answered Nov 04 '22 04:11

Richard Smith