从 Nginx 中的 URL 中删除 index.php

问题描述 投票:0回答:1

我的网址应采用以下格式:

https://www.example.com/author/article-title

但谷歌也尝试扫描:

https://www.example.com/index.php/author/article-title

我尝试使用以下规则在 Nginx 上解决此问题:

location /index.php {
    try_files $uri/$1/$2 "$uri/index.php\/([a-z-0-9-]{2,})\/([a-z-0-9-]{2,})\/?";
}

如果我尝试正确的 URL 有效,如果我尝试错误的 URL,服务器会响应错误 500。我想要重定向 301 到正确的 URL

regex nginx digital-ocean
1个回答
0
投票

我解决了(感谢建议):

机器人.txt

User-agent: *
Disallow: /index.php

nginx.conf

location / {
    # Redirect requests containing 'index.php' to the same URL without it
    if ($request_uri ~* "/index.php(.*)") {
        return 301 $1$is_args$args;
    }

    try_files $uri $uri/ /index.php?$query_string;
}
© www.soinside.com 2019 - 2024. All rights reserved.