how to make nginx redirect

I'm trying to do the following: There is a site where you can find a user in the format http://localhost/username . I tried to make a rule that if there is no file on the server, then it will redirect to index.php?username=username. But the problem is that it redirects everything, even js and css. I want to do it like this, if the file was on the server, then it would access it, and if not, then it would make a request

index.php?username=username

There is this piece of code:

rewrite ^/(.+)$ /index.php?type=userinfo&username=$1 last;

Upvotes: 1

Views: 308

Answers (1)

Richard Smith
Richard Smith

Reputation: 49702

Use try_files to determine if the file already exists, and branch to a named location containing your rewrite rule, if it does not.

For example:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^/(.+)$ /index.php?type=userinfo&username=$1 last;
}

Upvotes: 1

Related Questions