Rewrite all requests to index.php with nginx

后端 未结 8 2209
陌清茗
陌清茗 2020-11-28 05:17

In my apache configuration I have the following simple rewrite rule which

  1. unless file exists will rewrite to index.php
  2. on the urls you never see the f
相关标签:
8条回答
  • 2020-11-28 05:53

    If you want to pass just the index.php ( no other php file will be passed to fastcgi ) to fastcgi in case you have routes like this in a framework like codeigniter

    $route["/download.php"] = "controller/method";
    
    
    location ~ index\.php$ {
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi.conf;
    }
    
    0 讨论(0)
  • 2020-11-28 05:55

    1 unless file exists will rewrite to index.php

    Add the following to your location ~ \.php$

    try_files = $uri @missing;
    

    this will first try to serve the file and if it's not found it will move to the @missing part. so also add the following to your config (outside the location block), this will redirect to your index page

    location @missing {
        rewrite ^ $scheme://$host/index.php permanent;
    }
    

    2 on the urls you never see the file extension (.php)

    to remove the php extension read the following: http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/

    and the example configuration from the link:

    location / {
        set $page_to_view "/index.php";
        try_files $uri $uri/ @rewrites;
        root   /var/www/site;
        index  index.php index.html index.htm;
    }
    
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/site$page_to_view;
    }
    
    # rewrites
    location @rewrites {
        if ($uri ~* ^/([a-z]+)$) {
            set $page_to_view "/$1.php";
            rewrite ^/([a-z]+)$ /$1.php last;
        }
    }
    
    0 讨论(0)
提交回复
热议问题