By saeeddarabi
Hi everyone,
Server runs on Ubuntu 16.04.1 x64. Using WordPress. If more info needed, I’d be happy to provide.
I’m trying to redirect an old post to a new one but I’m having a lot of issues. I tried many codes and none worked so far.
Since the server uses Nginx , obviously I can’t use the usual 301 redirect codes that work with Apache.
I used this code a while back to redirect from http to https version for the whole site.
# BEGIN redirecting www to non www version
RewriteCond %{HTTP_HOST} =www.example.com
RewriteRule (.*) https://example.com/$1 [R=302,L]
# END redirecting www to non www version
It worked for that, so I tried modifying it and using it to also 301 an old post to a new one but it doesn’t work. There are no errors, but it just doesn’t work.
I also tried these codes (found by searching on Google) and almost all of them gave me a 500 Internal error for the whole site.
# BEGIN 301 Redirects
Redirect 301 /example.com/oldpost/
https://example.com/newpost/
# END 301 Redirects
# BEGIN 301 Redirects
rewrite ^/$ https://example.com/oldpost/ permanent;
rewrite ^/(.*)$ https://example.com/newpost/$1 permanent;
# END 301 Redirects
# BEGIN 301 Redirects
server {
listen 80;
server_name https://example.com/oldpost/;
return 301 $scheme:example.com/newpost/$request_uri;
}
# END 301 Redirects
# BEGIN 301 Redirects
server {
listen 80;
listen [::]:80;
hostname https://example.com/oldpost/
https://example.com/oldpost/;
return 301 https://example.com/newpost/$request_uri;
}
# END 301 Redirects
I know enough to research, find and copy-paste a piece fo code but beyond that I’m at a loss. I would appreciate any help.
Thanks very much in advance.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi @saeeddarabi,
It seems like since you are using Nginx it would be good to have more knowledge about it. Before I write anything else, I’ll recommend you going over the following to DigitalOcean tutorials:
Both tutorials will provide you the knowledge you’ll need to fully understand how server blocks in Nginx work.
Now, to your problem. You’ll need to add a 301 redirect in your server block for your domain. So let’s assume your domain is example.com. Firstly the conf should be located in the folder /etc/nginx/sites-available/example.com. Open it with your editor, which ever you prefer
nano /etc/nginx/sites-available/example.com
In there, you’ll see something in the midst of
server {
listen 80;
listen [::]:80;
root /var/www/example.comm/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
Under the server_name and above the location block, you’ll need to add the following line
rewrite ^/products.html$ /offer.html permanent;
Of course, you’ll need to change the URLs with the first one, in this case -products.html to be your old URL and the second one to be the new one.
Regards, KDSys
Hi KDSys,
Thanks very much for taking the time to write such a detailed response. Much appreciated.
Yes, you are absolutely right in that I do need to learn more about Nginx. I’m slowly doing it and learning more as I go along.
Thanks for linking to the two guides. Immensely helpful.
I also appreciate your specific solution to my problem. Since I use Server Pilot to manage the server, I can’t access the ETC folder. I have to change some settings I guess, so I am working to do that now. Once that’s done I’ll use the code you provided.
Once again, thanks very much for taking the time out of your busy day to help out.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.