Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx rewrite redirect for a folder

Tags:

all...

I am trying to do something in nginx to redirect all calls for files in

/images/ 

to become in:

/assets/images/ 

can someone help me with the rewrite rule? giving a 301 moved permanently status?

like image 839
alybadawy Avatar asked Aug 19 '13 01:08

alybadawy


People also ask

What is return 301 in NGINX?

Temporary and Permanent Nginx Redirect Explained To map this change, the redirects response code 301 is used for designating the permanent movement of a page. These kinds of redirects are helpful when the user wants to change the domain name and no longer wants a browser to access it.

What is $URI in NGINX?

It is exactly the $uri/ part that makes nginx assuming an URI can be a directory name and looking for an index file presence inside it.

What is Try_files in NGINX?

The try_files directive exists for an amazing reason: It tries files in a specific order. NGINX can first try to serve the static content, and if it can't, it moves on. This means PHP doesn't get involved at all.


2 Answers

Here's the preferred way to do this with newer versions of Nginx:

location ~ ^/images/(.*) {     return 301 /assets/images/$1; } 

See https://www.nginx.com/blog/creating-nginx-rewrite-rules/ for more info.

like image 187
gsf Avatar answered Dec 11 '22 04:12

gsf


Add below configuration into your nginx.conf

rewrite ^/(images.*) /assets/$1 permanent; 
like image 22
oldmonk Avatar answered Dec 11 '22 05:12

oldmonk