Nginx 重写:我该如何重写它?
我已经呆了几个小时了 - 没有什么可用,所以我在这里。
我在/页/中有一堆文件,即123.php
,321.php
,111.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试不使用“=”符号“如果使用等号,如果请求 URI 与给定的位置完全匹配,则该块将被视为匹配。”
像这样
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