Nginx 重写:我该如何重写它?

发布于 2025-01-19 03:18:48 字数 3087 浏览 0 评论 0原文

我已经呆了几个小时了 - 没有什么可用,所以我在这里。

我在/页/中有一堆文件,即123.php321.php111.php

。需要重写URL,以便在访问此URL时显示这些文件的内容:

example.com/index.php?id={filename-without-.php}

so:so:

example.com/pages/123.php = => example.com/index.php?id=123

example.com/pages/999.php => example.com/index.php?id=999

我正在这样做以保留原始URL。

经过大量的阅读和反复试验,我想到了:

location = /pages {
    rewrite ^/pages/([0-9]+)?$.php index.php?id=$1 permanent;
}

它仍然不起作用,我不知道为什么不弄清楚。

如果我访问example.com/index.php?id=123我只是重定向到example.com/?id=123和我的WordPress安装的首页正在显示。

任何帮助都将不胜感激 - 谢谢,谢谢。

##########################

server {
        server_name example.com www.example.com;
        listen 130.x.x.x;
        ######## HTTP -> HTTPS redirect ###################
        #set $redirect_var 0;
        #        if ($https = '') {
        #        set $redirect_var 1;
        #        }
        #        if ($redirect_var = 1) {
        #return 301 https://www.example.com$request_uri;
        #        }
        ###################################################
        root /home/example.com/public_html;
        index index.php index.htm index.html;
        access_log /var/log/virtualmin/example.com_access_log;
        error_log /var/log/virtualmin/example.com_error_log;
        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param SERVER_SOFTWARE nginx;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_param SCRIPT_FILENAME /home/example.com/public_html$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT /home/example.com/public_html;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param HTTPS $https;
        #location ~ \.php(/|$) {
        #   try_files $uri $fastcgi_script_name =404;
        #   fastcgi_pass unix:/var/php-nginx/164846487416392.sock/socket;
        #}
        # htaccess
        #location / {
        #   try_files $uri $uri/ /index.php?$args;
        #}
        location /pages {
            rewrite ^/pages/(.*)$.php /index.php?id=$1 permanent;
        }
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        listen 130.x.x.x:443 ssl;
        ssl_certificate /home/example.com/ssl.combined;
        ssl_certificate_key /home/example.com/ssl.key;
    }

I've been at this for hours now - nothing works, so here I am.

I have a bunch of files in /pages/ with number filenames, i.e. 123.php, 321.php, 111.php etc.

I need to rewrite the URL so that the content of these files are shown when accessing this URL:

example.com/index.php?id={filename-without-.php}

So:

example.com/pages/123.php => example.com/index.php?id=123

example.com/pages/999.php => example.com/index.php?id=999

I'm doing this to preserve the original URLs.

After a lot of reading and trial/error, I've come up with:

location = /pages {
    rewrite ^/pages/([0-9]+)?$.php index.php?id=$1 permanent;
}

It still doesn't work, and I can't figure out why not.

If I access example.com/index.php?id=123 I'm merely redirected to example.com/?id=123 and the front page of my WordPress install is showing.

Any help would be GREATLY appreciated - thank you, thank you.

##########################

Full server-block with all rules:

server {
        server_name example.com www.example.com;
        listen 130.x.x.x;
        ######## HTTP -> HTTPS redirect ###################
        #set $redirect_var 0;
        #        if ($https = '') {
        #        set $redirect_var 1;
        #        }
        #        if ($redirect_var = 1) {
        #return 301 https://www.example.com$request_uri;
        #        }
        ###################################################
        root /home/example.com/public_html;
        index index.php index.htm index.html;
        access_log /var/log/virtualmin/example.com_access_log;
        error_log /var/log/virtualmin/example.com_error_log;
        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param SERVER_SOFTWARE nginx;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_param SCRIPT_FILENAME /home/example.com/public_html$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT /home/example.com/public_html;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param HTTPS $https;
        #location ~ \.php(/|$) {
        #   try_files $uri $fastcgi_script_name =404;
        #   fastcgi_pass unix:/var/php-nginx/164846487416392.sock/socket;
        #}
        # htaccess
        #location / {
        #   try_files $uri $uri/ /index.php?$args;
        #}
        location /pages {
            rewrite ^/pages/(.*)$.php /index.php?id=$1 permanent;
        }
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        listen 130.x.x.x:443 ssl;
        ssl_certificate /home/example.com/ssl.combined;
        ssl_certificate_key /home/example.com/ssl.key;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2025-01-26 03:18:48

请尝试不使用“=”符号“如果使用等号,如果请求 URI 与给定的位置完全匹配,则该块将被视为匹配。”

像这样

location /pages {

rewrite ^/pages/(.*)$.php /index.php?id=$1 permanent;

}

please try it without the '=' sign " If an equal sign is used, this block will be considered a match if the request URI exactly matches the location given."

to be like this

location /pages {

rewrite ^/pages/(.*)$.php /index.php?id=$1 permanent;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文