【问题标题】:nginx redirect all URI requests with query strings/arguments to the equivalent withoutnginx 将所有带有查询字符串/参数的 URI 请求重定向到等价的没有
【发布时间】:2015-11-11 00:59:30
【问题描述】:

我正在阅读有关通过重定向(例如 $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 url-rewriting rewrite


    【解决方案1】:

    我看到你正在使用我来自nginx php friendly URL redirection without interfering with index.php causing /index 的解决方案! :-)

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

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

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 2019-06-17
      • 2017-04-10
      • 2011-03-16
      • 2015-12-13
      相关资源
      最近更新 更多