Nginx Rewrite 301 vs Return?

Chris Worner

Well-known member
Joined
Apr 15, 2016
Messages
366
Best answers
0
Ratings
35
Points
28
#1
I have a NGINX on my VPS server, I want to know what is the difference between Rewrite 301 and Return

for example.

Code:
rewrite ^ http://www.example.com/urlvar/$request_uri? permanent;
Return 301:
Code:
location ~ redirect-this/?$ {
    return 301 http://www.example.com/urlvar/redirect-this$1;
}
Which method is more effective to use to make a 301 redirection? what would you recommend?

Thanks in advance.
 

VirtuBox

Global Mod
Staff Member
Joined
May 3, 2016
Messages
1,551
Best answers
4
Ratings
412 13
Points
83
#2
I have a NGINX on my VPS server, I want to know what is the difference between Rewrite 301 and Return

for example.

Code:
rewrite ^ http://www.example.com/urlvar/$request_uri? permanent;
Return 301:
Code:
location ~ redirect-this/?$ {
    return 301 http://www.example.com/urlvar/redirect-this$1;
}
Which method is more effective to use to make a 301 redirection? what would you recommend?

Thanks in advance.
Hello @Chris Worner ,
it's a common mistake to use rewrite directive with nginx instead of return, because nginx rewrite directive isn't the same than apache rewrite directive.

An example of return directive usage :
Code:
# redirect old url to new url 
location /your-old-url {
return 301 https://yourdomain.tld/yournewurl;
}
An example of rewrite directive usage :
Code:
 # remove .html at the end of url 
location / {
  rewrite ^(/.*)\.htm(\?.*)?$ $1$2 permanent;
  try_files $uri $uri/ /index.php?$args;
}
 
Joined
Jan 19, 2013
Messages
33
Best answers
1
Ratings
9
Points
8
#4
Rewrite rules change part or the entire URL in a client request, usually for one of two purposes:
To address the clients about the resources they are requesting now resides at various locations. Say for instance, apply cases are when your website’s domain name has changed, when you want clients to use a canonical URL format also when you want to catch and spellcheck misspellings of your domain name. The return and rewrite directives are suitable for these purposes.

server {
listen 80;
listen 443 ssl;
server_name www.old-name.com;
return 301 $scheme://www.new-name.com$request_uri;
}
 
Older Threads
Replies
2
Views
116
Replies
4
Views
208
Replies
4
Views
148
Recommended Threads
Replies
6
Views
1,725
Replies
7
Views
1,693
Replies
0
Views
1,289
Replies
1
Views
1,428

Latest Hosting OffersNew Reviews

Sponsors

Latest Blog ArticlesMost Viewed Threads

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top