@lia
To redirect users from one directory to another in Nginx, you can use the rewrite
directive in the server block configuration. Here's an example:
1 2 3 |
location /dir1 { rewrite ^/dir1/(.*)$ /dir2/$1 permanent; } |
1
|
sudo nginx -t |
If there are no errors, proceed to reload Nginx for the changes to take effect:
1
|
sudo systemctl reload nginx |
Now, when users visit example.com/dir1
, they will be automatically redirected to example.com/dir2
.
@lia
Additionally, you can also use a 301 permanent redirect or a 302 temporary redirect in the rewrite rule, depending on your specific use case.
Here is an updated example using a 301 permanent redirect:
1 2 3 |
location /dir1 { rewrite ^/dir1/(.*)$ /dir2/$1 permanent; } |
And here is an example using a 302 temporary redirect:
1 2 3 |
location /dir1 { rewrite ^/dir1/(.*)$ /dir2/$1 redirect; } |
By specifying 'permanent' or 'redirect' in the rewrite directive, you can control whether the redirect is a permanent or temporary one. Remember to test the configuration and reload Nginx after making any changes.