Nginx Rewrite 301 vs Return?

Chris Worner

Well-known member
Joined
Apr 15, 2016
Messages
331
Best answers
0
Ratings
31
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,517
Best answers
4
Ratings
392 12
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
19
Best answers
0
Ratings
3
Points
3
#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
4
Views
135
Replies
4
Views
86
Latest Threads
  • Deleted
  • Deleted by Localnode
  • Reason: Wrong section
Replies
0
Views
4
  • Deleted
  • Deleted by fwh
Replies
0
Views
1
Replies
13
Views
83

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