微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel 5 nginx domain.com/index.php和domain.com/都可以使用.如何重定向/index.php?

在树莓派2上安装了NginxPHP 5.6.工作奇妙地减去了我的页面仍然从xyz.com/index.PHP/whatever(WRONG)以及xyz.com/whatever(PERFECT)加载的事实.我不希望/index.PHP加载我的主页.我希望它重定向到/不带index.PHP.这适用于所有子文件夹.我究竟做错了什么??这是我建造的第一台服务器,所以任何帮助都会受到赞赏.谢谢.

网站可用conf:

server {
    root /data/something.com/www/something/public;
    index index.PHP index.html index.htm;
    server_name something.com.local;    

    # Logging
    error_log /data/something.com/logs/error.log;
    access_log /data/something.com/logs/access.log;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # disallow access to hidden files
    location ~ /\. {
        deny all;
    }

    # Some locations will never contain PHP files, handle it in the server
    location ~ ^/(robots.txt|favicon.ico)(/|$) {
        try_files $uri =404;
    }

    # Pretty urls no /index.PHP (THIS IS WORKING...BUT /index.PHP is still accessible?)
    location / {
            try_files $uri $uri/ /index.PHP?$query_string;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
            rewrite     ^/(.+)/$/$1 permanent;
    }   

    error_page 404 /index.PHP;

    sendfile off;

    # PHP       
    location ~ \.PHP${
            fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
            fastcgi_pass unix:/var/run/PHP5-fpm.sock;
            fastcgi_index index.PHP;
            include /etc/Nginx/fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with Nginx.
    location ~ /\.ht {
            deny all;
    }
}

解决方法:

正如在nginx redirect loop, remove index.php from url中解释的和Remove index.php from Joomla! URLs with NGINX类似,当从index.PHP重定向时,你必须使用特殊技巧来避免重定向循环:

index index.PHP index.html index.htm;
if ($request_uri ~ "^/(.*)(?<=/)index\.PHP[/?]?((?<=[/?]).*)?$") {  return  301 /$1$2;  }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐