nginx将所有具有查询字符串/参数的URI请求重定向到不具有查询字符串或参数的对等请求



我读过关于通过像$1?$argument$1这样的重定向从所有请求中删除查询字符串/参数的文章。

我尝试了文档中所说的在所需重写的末尾添加?以删除查询字符串和参数,但没有效果。

我希望维护当前的功能,并从所有URI请求中删除查询字符串/参数。

什么与文档中关于添加?的建议相冲突,或者正确的解决方案是什么?

# 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;
}

我看到你正在使用我的解决方案,该解决方案来自nginx.php友好的URL重定向,而不会干扰index.php导致/index!:-)

如果你还想去掉整个$args/$query_string,除了去掉index.php之外,那么只需从提供的解决方案中省略$2;此外,您还可以有一个额外的if条件,用于从其余URL中删除参数。

index index.php;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:.php)?|.php)(?.*)?$) { return 301 /$1; }
if ($request_uri ~ ^/([^?]*)?) {   return 301 /$1; }

最新更新