Nginx conf how to remove leading slash from $ uri

My Nginx conf file:

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

  ## PHP conf in case it 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;
  }

Try 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

+4
source share
1 answer

Use name and internal rewriting. For instance:

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

See this document for more details .

+5
source

Source: https://habr.com/ru/post/1668092/


All Articles