weblog
weblog copied to clipboard
Simplify Nginx Rewrite Rules and Move Logic to Code
Currently, the Nginx configuration contains a set of rewrite rules to handle various URL patterns. The configuration for these rewrite rules looks like this:
location @rewrite {
rewrite ^/(.+)\.txt/$ $scheme://$host/$1.txt permanent;
rewrite ^/(.+)\.txt$ /index.php?go=$1 last;
rewrite ^/([^/]+)$ $scheme://$host/$1/ permanent;
rewrite "^/(\d{4})$" /$1/ permanent;
rewrite "^/(\d{4})/(\d{2})$" /$1/$2/ permanent;
rewrite "^/(\d{4})/(\d{2})/(\d{2})$ /$1/$2/$3/ permanent;
rewrite ^/rss/([\w-]+)$ /rss/$1/ permanent;
rewrite ^/latest$ /latest/ permanent;
rewrite ^/latest/([^/]+)$ /latest/$1/ permanent;
rewrite ^/search/(.*)/?$ /index.php?go=search/$1 last;
rewrite ^/selected$ /selected/ permanent;
rewrite ^/drafts/([^/]+)$ /drafts/$1/ permanent;
rewrite ^/(.*)/$ /index.php?go=$1 last;
}
To simplify and improve the maintainability of the application, I suggest leaving only the following rewrite rule in the Nginx configuration:
rewrite ^/(.*)/$ /index.php?go=$1 last;
And moving the rest of the rewrite logic into the application code itself. This way, the URL handling can be more flexible and easier to manage directly within the application's routing logic.