Using Apache
I’d go with an .htaccess
rule
RewriteRule ^test/(.*) http://www.example.com/demo/$1 [R=301,L]
so http://www.example.com/test/page
would now go to http://www.example.com/demo/page
and the 301 redirect would make it permanent
make sure you put your rewrite rule after these rules
RewriteEngine on
RewriteBase /
Using Nginx
in the server block
server {
. . .
rewrite ^/test/(.*) http://www.example.com/demo/$1 permanent;
. . .
}