NGINX rewrite for vanity URLs when file doesn't exist (try_files and rewrite together)

Posted by user1721724 on Server Fault See other posts from Server Fault or by user1721724
Published on 2013-06-29T13:51:14Z Indexed on 2013/06/29 16:22 UTC
Read the original article Hit count: 310

Filed under:
|

I'm trying to get vanity URLs on my server. If the file path from the URL doesn't exist, I want to rewrite the URL to profile.php, but if my users have periods in their usernames, their vanity URL doesn't work.

Here is my conf block.

server {
    listen       80;
    server_name  www.example.com;

    rewrite ^/([a-zA-Z0-9-_]+)$ /profile.php?url=$1 last;

    root   /var/www/html/example.com;
    error_page 404 = /404.php;

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 1y;
        log_not_found off;
    }

    location ~ \.php$ {
        fastcgi_pass  example_fast_cgi;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/example.com$fastcgi_script_name;
        include        fastcgi_params;
    }

    location / {
        index  index.php index.html index.htm;
    }

    location ~ /\.ht {
        deny  all;
    }

    location /404.php {
        internal;
        return 404;
    }
}

Any help would be appreciated. Thanks!

© Server Fault or respective owner

Related posts about nginx

Related posts about rewrite