I've been trying to rewrite some Nginx location urls but can't get it to work.
For the directory structure /dir1/dir2/index.html, where I will have several different dir2 directories and dir1 is just for organizational purposes, I want to rename the urls to:
mysite.com/dir2
This is my best guess.
location ~ /dir1.*$ {
rewrite ^/dir1/(.*)/.*$ mysite.com/$1 break;
}
I've tried several variations on this, and I can't get anything to change at all. Clicking on the link just results in:
mysite.com/dir1/dir2/index.html
Here is the full config:
server {
root /var/www/website;
index index.html;
server_name mysite.com www.mysite.com;
location / {
try_files $uri $uri/ =404;
}
location ~ ^/dir1.*$ {
rewrite ^/dir1/(.*)/.*$ mysite.com/$1 break;
}
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = www.mysite.com) {
return 301 https://$host$request_uri;
}
if ($host = mysite.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name mysite.com www.mysite.com;
return 404;
}
I check and restart Nginx every time. Maybe the Certbot config is interfering. What am I doing wrong?