Nginx Rewrite 301 vs Return?

Chris Worner

Well-known member
Registered
Joined
Apr 15, 2016
Messages
612
Points
28
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

Well-known member
Registered
Joined
May 3, 2016
Messages
1,622
Points
83
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;
}
 

HostechSupport

Active member
Registered
Joined
Jan 19, 2013
Messages
68
Points
8
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
1,390
Replies
4
Views
3,278
Replies
4
Views
1,187

Latest Hosting OffersNew Reviews

Sponsors

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