Rewrite all requests to index.php with nginx

后端 未结 8 2208
陌清茗
陌清茗 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:32

    Here is what worked for me to solve part 1 of this question:

        location / {
                rewrite ^([^.]*[^/])$ $1/ permanent;
                try_files $uri $uri/ /index.php =404;
                include fastcgi_params;
                fastcgi_pass php5-fpm-sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
        }
    

    rewrite ^([^.]*[^/])$ $1/ permanent; rewrites non-file addresses (addresses without file extensions) to have a "/" at the end. I did this because I was running into "Access denied." message when I tried to access the folder without it.

    try_files $uri $uri/ /index.php =404; is borrowed from SanjuD's answer, but with an extra 404 reroute if the location still isn't found.

    fastcgi_index index.php; was the final piece of the puzzle that I was missing. The folder didn't reroute to the index.php without this line.

    0 讨论(0)
  • 2020-11-28 05:34

    Flat and simple config without rewrite, can work in some cases:

    location / {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /home/webuser/site/index.php;
    }
    
    0 讨论(0)
  • 2020-11-28 05:36

    Using nginx $is_args instead of ? For GET query Strings

    location / { try_files $uri $uri/ /index.php$is_args$args; }
    
    0 讨论(0)
  • 2020-11-28 05:37

    To pass get variables as well use $args:

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    0 讨论(0)
  • 2020-11-28 05:39

    Here's the answer of your 2nd question :

       location / {
            rewrite ^/(.*)$ /$1.php last;
    }
    

    it's work for me (based my experience), means that all of your blabla.php will rewrite into blabla

    like http://yourwebsite.com/index.php to http://yourwebsite.com/index

    0 讨论(0)
  • 2020-11-28 05:44

    Perfect solution I have tried it and succeed to get my index page when I have append this code in my site configuration file.

    location / {
                try_files $uri $uri/ /index.php;
    }
    

    In configuration file itself explained that at "First attempt to serve request as file, then as directory, then fall back to index.html in my case it is index.php as I am providing page through php code.

    0 讨论(0)
提交回复
热议问题