简体   繁体   中英

Nginx rewrite rules are not working as expected

I have a website https://dev.mywebsite.com/index.php?album=portraits which displays photo-albums dynamically based on a POST value.

I want to rewrite this URL to this: https://dev.mywebsite.com/portraits

But my Nginx rewrite rule is not working. Nothing is happening when I enter https://dev.mywebsite.com/index.php?album=portraits . And no page is found when entering https://dev.mywebsite.com/portraits .

I don't know what I am doing wrong.

This is the code I'm trying to use currently:

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

I've also tried this:

location = /portraits { 
        rewrite ^/portraits?$ /index.php?album=portraits break; 
}

and this:

location = /album { 
        rewrite ^album/([a-z]+)/?$ album.php?album=$1 break; 
}

This is the entire nginx site-config file i'm using:

server {
        listen 80 default_server;
        listen 443 ssl;                                         
        root /config/www;
        index index.php index.htm index.html;
        server_name dev.mywebsite.com;

        ssl_certificate /config/keys/cert.crt;
        ssl_certificate_key /config/keys/cert.key;

        client_max_body_size 0;

        location / {
                try_files $uri $uri/ /index.html /index.php?$args =404;
        }
    location / {
                rewrite ^/(.*)$ /album.php?album=$1 last;
        }

    location ~ \.php$ {
                #fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # With php5-cgi alone:
                fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include /etc/nginx/fastcgi_params;

        }
}

If the file you really have is index.php , then you can rewrite (without any additional location ) with:

rewrite ^/portraits$ /index.php?album=portraits last;

Keep the location ~ \\.php$ { of course :)

Im added my ngix file but not working, please can help me.

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
rewrite ^/portraits$ /search.php?q=portraits last;

}

Thank you in advance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM