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

时间:2015-09-11 17:33:38

标签: php redirect nginx laravel-5

在树莓派2上安装了nginx和php 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;
    }
}

1 个答案:

答案 0 :(得分:0)

正如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;  }