nginx - 将 index.php?hello=x 重写为 index/hello/x [英] nginx - rewriting index.php?hello=x to index/hello/x

查看:52
本文介绍了nginx - 将 index.php?hello=x 重写为 index/hello/x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 nginx 将 index.php?question=x 重写为 index/question/x

How can I rewrite index.php?question=x to index/question/x using nginx

使 index.php?question=x 返回 404 未找到(如果可能)

make index.php?question=x return 404 not found (if possible)

从 URL 中完全删除 .php 文件扩展名

completely remove .php file extension from URLs

我确信所有这些都在某处被覆盖,但我无法理解 nginx 站点上的重写.

I'm sure all of this has been covered somewhere but I have trouble understanding the rewrites on the nginx site.

推荐答案

如果 index.php 确实存在并且你想重写它,你不能让 index.php 返回 404 错误.

You can't make index.php return a 404 error if index.php actually exists and you want to be rewriting to it.

这两个……

rewrite ^index/question/(.*)$ /index.php?question=$1 last;

location = /index.php {
    return 404;
}

...还有这个...

rewrite ^(.*)/(.*)/(.*)$ /$1.php?$2=$3 last;

location ~* \.php$ {
    return 404;
}

... 将为每个index/question/x"请求返回 404 错误.这是因为重写机制只是网络服务器对重写资源发出后台请求的情况.

... will return 404 errors for every "index/question/x" request. This is because the rewrite mechanism is simply a case of the webserver issuing a background request for the rewritten resource.

因此,index/question/x"将生成一个index.php?question=x"后台请求,该请求将命中该位置,表示应返回404"错误.

So "index/question/x" will generate an "index.php?question=x" background request which will hit the location saying that a "404" error should be returned.

如果你想从你的 url 中完全摆脱 index.php,你需要让你的应用程序停止生成 index.php 链接.如果您不创建此类链接,它们将不再使用.

If you want to completely get rid of index.php from your urls, you need to make your application stop generating index.php links. If you are not creating such links, they will go away from use.

你可以在 Nginx 和你的应用程序之间做一些事情来加快这个过程.

You could do something between Nginx and your application to quicken the process though.

  1. 首先,将您的应用程序更改为始终生成索引/问题/x"链接
  2. 向在 Nginx 中重写的/index.php?question=x"添加一个虚拟标签,以便您的应用程序知道这是一个源自 Nginx 的后台请求.
  3. 设置您的应用程序以测试该虚拟标签是否存在,对于它收到的任何/index.php?question=x"类型的请求.如果存在,则提供请求,如果没有,则重定向到index/question/x"网址.
  4. 我们还需要 Nginx 中的虚拟键(apptag")来识别这些类型的链接,并允许我们在它们自己的位置处理这些链接,而无需我们的重写弄乱其他合法链接.

在 Nignx 中:

# Any request uri starting with "/app_tag/" is given the treatment
location ~* ^/app_tag/ {
    rewrite ^/app_tag/(.+)/(.+)/(.+)$ /$1.php?$2=$3&dummy_tag last;
    rewrite ^/app_tag/(.+)/?$ /$1.php?dummy_tag last;
}
location ~* \.php$ {
    #proxy_pass/fastcgi_pass etc
}

在您的应用程序中:

$dummy_tag = "dummy_tag";
$app_tag = "app_tag";
$test_dummy_tag = preg_match($dummy_tag, $_SERVER['REQUEST_URI']) ? TRUE : FALSE;
if (!$test_dummy_base_tag) {
    // Dummy tag not present so we need redirect user
    $pattern_all = '~^/(.+)\.php\?(.+)=(.+)~si';
    $test_pattern_all = preg_match($pattern_all, $_SERVER['REQUEST_URI']) ? TRUE : FALSE;
    if ($test_pattern_all) {
        preg_match($pattern_all, $_SERVER['REQUEST_URI'], $matches);
        $pattern_new = "/apptag/" . $matches[1] . "/" . $matches[2] . "/" . $matches[3] . "/";
    } else {
        $pattern = '~^/(.+)\.php$~si';
        $test_pattern = preg_match($pattern, $_SERVER['REQUEST_URI']) ? TRUE : FALSE;
        if ($test_pattern) {
            preg_match($pattern, $_SERVER['REQUEST_URI'], $matches);
            $pattern_new = "/" . $app_tag . "/" . $matches[1] . "/";
        }
    }
    header('HTTP/1.1 301 Moved Permanently');
    header (location: "http://" . $_SERVER['HTTP_HOST'] . $pattern_new);
}

您可以根据需要修改/扩展它.

You can modify/extend this as required.

这篇关于nginx - 将 index.php?hello=x 重写为 index/hello/x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆