1

I was reading around about removing query strings/arguments from all requests by way of redirection like $1?$argument to just $1.

I tried what the documentation said in adding a ? to the end of the desired rewrite in order to remove query strings and arguments, but that had no effect.

I wish to maintain current functionality, and remove query strings/arguments from all URI requests.

What is conflicting with the documentation's suggestion of appending ?, or what is the correct solution for this?

# for removing .php from all requests
location / {
try_files $uri $uri/ @extensionless-php;
}

# rewrite to remove .php
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}

# deny access to php includes
location ~* /includes/(.+)\.php$ {
deny all;
}

# redirect all instances of index.php to / in its respective directory (for example, /index.php to /, and /articles/index.php to /articles/)
location ~* \.php {
try_files $uri =404;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1$2; }
fastcgi_pass backend;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Dogbert
  • 77
  • 1
  • 8

1 Answers1

0

I see you're using my solution from nginx php friendly URL redirection without interfering with index.php causing /index! :-)

If you also want to get rid of the whole $args / $query_string, in addition to removing index and .php, then just omit the $2 from the provided solutions; also, you can have an extra if-condition for removing the args from the rest of your URLs, too.

index index.php;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1; }
if ($request_uri ~ ^/([^?]*)\?) {   return 301 /$1; }
Community
  • 1
  • 1
cnst
  • 25,870
  • 6
  • 90
  • 122