【问题标题】:Nginx rewrite rule dynamic sub-folders and pathNginx重写规则动态子文件夹和路径
【发布时间】:2018-09-04 01:34:22
【问题描述】:

我正在尝试将 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

"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;
}

【问题讨论】:

    标签: linux wordpress nginx url-rewriting


    【解决方案1】:
    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/`
    

    【讨论】:

    • 我在配置文件中的任何其他“位置”之前添加了这些。当我尝试访问example.com/test/article/article-name 时,Wordpress 显示了它的主页。我的配置文件位于带有域名的“站点可用”文件夹中。在这种情况下,文件名为“example.com”。谢谢!
    • 我做了选择二。并且还创建了目录 test 和文件 index.html。仍然显示 Wordpress 的主页。
    • Larry,我在我的问题中添加了整个配置文件以及您的建议。谢谢。
    • 嗨拉里,你的建议是有效的(选择二)。我发现我的 index.html 有问题。由于我的声誉很低,我无法选择您的答案。谢谢。
    • 我觉得选择 II 更好,但对我不起作用……是否有任何情况下选择 II 不起作用
    猜你喜欢
    • 2020-02-17
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2020-07-29
    • 2012-11-22
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多