简体   繁体   中英

How to use rewrite in Nginx?

I use .htaccess in Apache2, it works well.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_]+)$ profile.php?username=$1

and now I use nginx rewrite, in sites-available, i make this config

server {
listen 80;
server_name domain.com;
root /home/domain;
index index.html index.htm index.php;
location / {
    rewrite "^([A-Za-z0-9_]+)$" profile.php?username=$1 last;
}
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
location ~ /\.ht {
    deny all;
}
}

but it doesn't work, it shows 404 Not Found, can you help me the error? thanks.

There is a issue with the rewrite translation. Check this updated rewrite translation in this code.

server {
listen 80;
server_name domain.com;
root /home/domain;
index index.html index.htm index.php;
location / {
if (!-e $request_filename){
rewrite ^/([A-Za-z0-9_]+)$ /profile.php?username=$1;
}
}
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
location ~ /\.ht {
    deny all;
}
}

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