Need help with Nginx 301 redirects

Hey everyone,

I have been with Linode since June 2009, on what used to be a 720 (now 1024) and I'm loving it.

Now, I'm trying to build a new web server, on Ubuntu 10.04 LTS, with PHP-FPM (Separate) Installed from Source and Nginx as the web server. I've got everything up and running, thanks to the_guv's brilliant guide.

Where I'm struggling and need help with are 301 redirects. I've got Wordpress running, with the proper permalinks, proper www vs non-www but I can't seem to figure out how to do a simple 301 redirect. Coming from using a wordpress plugin and apache .htaccess, I'm lost here.

Example: I'd to redirect

http://www.exampleurl.com/computer-syst … pc/1500-2/">http://www.exampleurl.com/computer-systems/home-theater-pc/1500-2/

to

http://www.exampleurl.com/1500-htpc/

Here's what my /usr/local/nginx/sites-available/exampleurl.com file looks like:

server {

            listen   80;

            server_name  exampleurl.com;

            rewrite ^/(.*) http://www.exampleurl.com/$1 permanent;

       }

server {

            listen   80;

            server_name www.exampleurl.com;

            access_log /home/username/public_html/exampleurl.com/log/access.log;

            error_log /home/username/public_html/exampleurl.com/log/error.log;

            location /  {

                        root   /home/username/public_html/exampleurl.com/public/;

                        index  index.php index.html;

# Wordpress WP Super Cache plugin and permalinks.
include /usr/local/nginx/conf/wordpress_params.super_cache;

                        }

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

            location ~ \.php$

                                {

            fastcgi_pass 127.0.0.1:9000;

            fastcgi_index index.php;

            include /usr/local/nginx/conf/fastcgi_params;

            fastcgi_param SCRIPT_FILENAME /home/username/public_html/exampleurl.com/public/$fastcgi_script_name;

                                }

      }

Basically, if you could show me an example of what code to insert and where to insert it, using the example above, I would greatly appreciate that. Thanks!

2 Replies

From your info there is no generic way since nginx doesn't know what the URL turns into. It is not just a change of the URL but a completely different one. But if you have a certain set of hard ones to rewrite it would be something like

location = /computer-systems/home-theater-pc/1500-2/ {

rewrite ^ /1500-htpc/ permanent;

}

And just repeat for all your preexisting urls.

Syntax might be slightly off. Going off pure memory.

Schultz,

That's exactly what I was looking for and after testing it, I can confirm that it works perfectly.

Thanks a lot for your help and time.

Cheers,

Mathieu

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct