php - Nginx 将所有请求从子目录重定向到另一个子目录根目录

标签 php redirect nginx

我是 Nginx 的新手,所以请多多包涵。

我正在尝试将所有请求从一个子目录(store)重定向到另一个子目录(trade)的根目录。在下面查看我的进度。目标子目录 (trade) 中的站点是一个 magento 站点,因此大多数当前规则都是针对该站点的。

server {
    server_name example.com *.example.com;
    root /usr/share/nginx/html/example.com/public_html;
    index index.php index.html index.htm;

    access_log /var/log/nginx/example.access.log;
    error_log /var/log/nginx/example.error.log;

    location / {
            try_files $uri $uri/ /index.html;
    }

    location /trade/ {
            index index.html index.php;
            try_files $uri $uri/ @handler;
            expires 30d;
    }

    location ~ /store {
            rewrite /trade permanent;
    }

    location ~ ^/trade/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
    location /trade/var/export/ { internal; }
    location /. { return 404; }
    location @handler { rewrite / /trade/index.php; }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;

    }


}

我用来重定向的部分如下:

location ~ /store {
        rewrite /trade permanent;
}

这适用于 example.com/store 但不适用于 example/store/index.php 或任何其他带有 args 的 uri。我有一种感觉,底部的 php 文件部分正在覆盖处理。这就是为什么我将 ~ 放在商店位置前面作为文档的原因 here声明这将首先处理。处理是停止还是继续?

我读过有关嵌套 php 规则的内容,但我已经尝试过但无济于事。

如果有任何帮助,我将不胜感激。

最佳答案

好吧,试试这样的东西

location ^~ /store(.*) {
  return 301 $scheme://$http_host/trade$1$is_args$query_string;
}

尽量避免硬编码的东西并使用 return 因为 it's prefered over permanent rewrites

关于php - Nginx 将所有请求从子目录重定向到另一个子目录根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22224441/

相关文章:

php - 将 WHERE 和 GROUP BY 合并到日期时间总和 MySQL 查询中

php - Linux/Apache 使用 PHP 与基于 Microsoft Dynamics 构建的 CRM 集成

带有 Express : how to redirect a POST request 的 Node.js

linux - 如何在 LAN Linux 中创建到另一台计算机的 tcp 重定向

nginx - 如何在 NGINX 配置中指定没有扩展名的文件的 mimetype?

php - imagick的getImageLength裁剪图像后无法正常工作

javascript - 建议将 Javascript 窗口名称传递给 php

angularjs - 用angular js表达js重定向

django - REMOTE_ADDR 未使用 nginx 和 Tornado 发送到 Django

nginx - 如何部署我的 Angular 2 + Typescript + Webpack 应用程序