【问题标题】:Laravel 5 nginx domain.com/index.php and domain.com/ both work. How to redirect /index.php?Laravel 5 nginx domain.com/index.php 和 domain.com/ 都可以工作。如何重定向/index.php?
【发布时间】:2015-12-08 08:21:14
【问题描述】:

在树莓派 2 上安装了 nginx 和 php 5.6。工作得非常好,但我的页面仍然从 xyz.com/index.php/whatever(错误)以及 xyz.com/whatever(完美)加载。我不希望 /index.php 加载我的主页。我希望它重定向到/没有index.php。这也适用于所有子文件夹。我究竟做错了什么??这是我建造的第一台服务器,因此我们将不胜感激。谢谢。

网站可用配置:

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

【问题讨论】:

    标签: php redirect nginx laravel-5


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      相关资源
      最近更新 更多