【问题标题】:Redirect users from page.php to page with nginx使用 nginx 将用户从 page.php 重定向到页面
【发布时间】:2015-10-25 06:37:41
【问题描述】:

我有以下服务器块:

server {
    listen 80;
    server_name petpal.co.il;
    root /usr/share/nginx/petpal;
    index index.php;
    rewrite ^/([a-zA-Z]+)\-([0-9\-]+)$ /$1.php?page=$2? last;
    rewrite ^/en/([a-zA-Z]+)\-([0-9\-]+)$ /en/$1.php?page=$2? last;
    location / {
        try_files $uri $uri/ @extensionless-php;
    }
    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }
    set $lang "";
    if ($uri ~ "^/en/") {set $lang "en/";}
    error_page 404 /${lang}notfound;
    location ~ \.php$ {
        try_files $uri =404;
        client_max_body_size 4M;
        fastcgi_read_timeout 300;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

它已经处理了 url 重写(将 petpal.co.il/dogs 处理为 dogs.php),但是,我想避免人们访问 page.php,因此不会有重复的页面(名为 x 和 x 的同一页面.php),所以我要做的就是添加一个规则,将人们从 page.php 重定向到没有 .php 的页面,或者至少显示一个 404 not found 页面(你认为什么更好?)

希望我解释得很好,有什么线索吗? 谢谢!

【问题讨论】:

    标签: php nginx url-rewriting


    【解决方案1】:

    我不确定我是否完全理解您的问题,但您可以执行以下操作,而不是编写规则。

    • 创建狗文件夹
    • 在其中创建一个 index.php 文件并编辑该文件

    现在,如果您访问 petpal.co.il/dogs,它将显示 petpal.co.il/dogs/index.php。

    用户仍然可以访问dogs/index.php,但它不是重复页面。

    希望对你有帮助!

    【讨论】:

    • 为每个文件创建一个目录?对我来说这听起来不是一个合适的解决方案,一定有一些事情可以用一个简单的规则来完成,无论如何谢谢:)
    • 我能想到的就这么多了,不知道还有什么办法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2014-02-28
    • 1970-01-01
    相关资源
    最近更新 更多