Nobody Nobody - 3 months ago 8
Apache Configuration Question

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.

Answer

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;
}
}