从htaccess迁移到NGINX配置

时间:2020-08-08 16:07:53

标签: php .htaccess nginx

今天,我从Apache迁移到Nginx + FPM,这导致htaccess不再起作用,我已经尝试过将htaccess应用于nginx转换器HERE,但并非100%导致了我之前想要的东西。

我在这里的目标(用htaccess充满)是在/之后将任何参数发送到index.php?menu =,但也可能有args:

accessing : example.com/index.php will change URL to example.com
    
accessing : example.com/test.php will change URL to example.com/test
  - server receive menu=test

accessing : example.com/fakepageaa.asd 
  - server receive receive menu=fakepageaa.asd
    
accessing : example.com/register 
  - server receive menu=register
    
accessing : example.com/welcome&args1=usa&args2=john 
  - server receive receive menu=welcome, args1=usa, args2=john

我的工作htaccess代码

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?menu=$1 [QSA]

RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

已更新

经过多次尝试,现在我的完整nginx配置是这样的

server {
    listen      IP:80;
    server_name example.com;
    root        /home/admin/web/example.com/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/example.com.log combined;
    access_log  /var/log/nginx/domains/example.com.bytes bytes;
    error_log   /var/log/nginx/domains/example.com.error.log error;
    autoindex off;

    location / { 

        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires     max;
        }

        if (!-e $request_filename){
            rewrite ^(.*)$ /index.php?menu=$1;
        }

        if (!-e $request_filename){
            rewrite ^/(.*?)/?$ /$1.php break;
        }

        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }

            fastcgi_pass    127.0.0.1:9002;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
    }

    error_page  403 /error_page.php;
    error_page  404 /error_page.php;
    error_page  500 502 503 504 /error_page.php;
    
    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }

    if ($request_uri ~* "^(.*/)index\.php(.*)") {
        return 307 $1$2;
    }
}

它几乎解决了我的问题,当前的问题:

accessing : example.com/fakepageaa.asd will direct to error page
  - server should be receive menu=/fakepageaa.asd

accessing : example.com/index will change URL to example.com
  - server should be receive menu=/index


accessing : example.com/test.php will change URL to example.com/test (solved)
  - server receive menu=/test (/ can be fix using str_replace)

accessing : example.com/welcome&args1=usa&args2=john (solved)
  - server receive menu=/welcome, args1=usa, args2=john (/ can be fix using str_replace)

accessing : example.com/register (solved)
  - server receive menu=/register (/ can be fix using str_replace)

accessing : example.com/index.php will change URL to example.com (solved)
     

如果有人可以帮助我,那将是有帮助的。 谢谢。

0 个答案:

没有答案