我正在尝试将 URL 模式重写为子文件夹。
location ~^ /test/article/(.*) {
root /var/www/example.com/test;
index index.html;
try_files $uri $uri/ /test/index.html;
}
当用户请求http://www.example.com/test/article/article-name时, 我需要将 URL 重写为 http://www.example.com/test/ ,但 URL 应保持不变,它应显示为 http://www.example.com/test/article/article-name在浏览器中。
“文章名称”可以是任何带破折号的内容。但“文章”是硬编码的路径,而不是真正的文件夹。
我的服务器当前在 Nginx 服务器的根目录中托管 Wordress,即/var/www/example.com/。我用 Nginx 配置文件尝试了不同的东西,但还没有工作。目前,当我输入http://www.example.com/test/article/article-name时,Wordpress会显示404错误。
如有任何帮助,我们将不胜感激。
谢谢。
更新::::::: 这是我在站点可用文件夹下的最新配置文件。当我尝试该 URL 时,它显示了 Wordpress 的主页。
server {
listen 8080 default_server;
listen [::]:8080 default_server;
port_in_redirect off;
set_real_ip_from 127.0.0.1/32;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
root /var/www/example.com;
index index.php index.html;
server_name example.com www.example.com;
rewrite ^/test/article/(.*) /test/;
location /test/ {
root /var/www/example.com/test/;
index index.html;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
另一个更新: 我现在正在使用以下配置进行测试,它不再显示 Wordpress 的 404。但它实际上重定向到/test 文件夹。我需要保留带有“/test/article/article-name”的网址。
rewrite ^/test/article/(.*) /test/;
location /test/ {
index index.html;
}
最佳答案
rewrite ^/test/article/(.*) /test;
location /test {
root /var/www/example.com/;
}
# create file `test` in `/var/www/example.com/`
选择二
rewrite ^/test/article/(.*) /test/;
location /test/ {
root /var/www/example.com/test/;
index index.html;
}
# create directory `test` in `/var/www/example.com/`
# and create file `index.html` in `/var/www/example.com/test/`
关于linux - Nginx重写规则动态子文件夹和路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49482442/