简体   繁体   中英

Nginx rewrite equivalent of apache rewrite

I am trying to change the configuration on my new Nginx server so it matches my current Apache settings. At the moment I am using this htaccess file:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*) index.php?url=$1 [L,QSA]  

I have found a converter that could "translate" this into the Nginx equivalent. I have tried the following:

location / {
    if (!-e $request_filename){
     rewrite ^/(.*) /index.php?url=$1 break;
    }
    try_files $uri $uri/ =404;
}

But when I try to set a url query like this http://domain.tld/something my php file gets returned and downloaded and that ain't supposed to happen.

What I expect to happen is when a url like http://domain.tld/something is entered it gets treated like http://domain.tld/index.php?url=something

Can someone tell me what I am doing wrong?

Unlike Apache, nginx doesn't come out-of-the-box ready to run PHP. You need to setup a handler to deal with php files, otherwise nginx will serve them up just like any regular file.

See: Nginx downloads php instead of running it

Also: review the NGINX documentation on using fast-cgi to run php .

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