NGINX rewrite for vanity URLs when file doesn't exist (try_files and rewrite together)
- by user1721724
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!