首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用nginx的wordpress内部重定向

使用nginx的wordpress内部重定向
EN

Server Fault用户
提问于 2021-09-17 02:11:45
回答 1查看 754关注 0票数 1

我有一个设置为Post名称的WordPress站点。

使用Nginx重写,我正在尝试内部重定向(浏览器URL没有改变),但到目前为止,我失败了。

This工作,但URL更改了

代码语言:javascript
复制
location ~ ^/u/(.*) {
    rewrite ^/u/(.*) /p/?username=$1 redirect; 
}

I不明白为什么这不起作用:

代码语言:javascript
复制
location ~ ^/u/(.*) {
    # this returns 404
    rewrite ^/u/(.*) /p/?username=$1 last;
}

The全局配置

代码语言:javascript
复制
server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    root /srv/www/html;
    error_log /var/log/nginx/error.log;
    index index.php;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ ^/u/(.*) {
        rewrite ^/u/(.*) /p/?username=$1 last;
        # try_files $uri /p/?username=$1;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        client_max_body_size 8M;
        include fastcgi-php.conf;

        # Mitigate https://httpoxy.org/ vulnerabilities
        fastcgi_param HTTP_PROXY "";
        fastcgi_intercept_errors on;
        fastcgi_pass 0.0.0.0:9000;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}

我正在编写一个WordPress插件,我希望用户输入https://example.com/u/john71之类的URL,并在内部重定向到https://example.com/p/?username=john71

up都是WordPress页面,p包含允许我检索用户名的短代码。

我可以使用https://example.com/p/?username=john71,但是类似于:https://example.com/u/john71看起来更好。

欢迎对重写规则或方法的任何帮助。

EN

回答 1

Server Fault用户

发布于 2021-09-17 07:07:50

URI最终被重写到/index.php,因此WordPress从未看到额外的内部重写。

WordPress解析原始请求,该请求在REQUEST_URI参数中传递给它(该参数将在fastcgi-php.conf文件中定义,其中可能包括来自另一个名为fastcgi_params的文件)。

您可以通过在调用REQUEST_URI之前直接设置fastcgi_pass和SCRIPT_FILENAME来完成这项工作--例如:

代码语言:javascript
复制
location ~ ^/u/(.*) {
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    fastcgi_param REQUEST_URI /p/?username=$1;

    fastcgi_param HTTP_PROXY "";
    fastcgi_intercept_errors on;
    fastcgi_pass 0.0.0.0:9000;
}

我还没有包括fastcgi-php.conf,因为它可能包含一个在这个location中不能工作的try_files语句。

票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1077842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档