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 … pc/1500-2/">


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

server {

            listen   80;


            rewrite ^/(.*)$1 permanent;


server {

            listen   80;


            access_log /home/username/public_html/;

            error_log /home/username/public_html/;

            location /  {

                        root   /home/username/public_html/;

                        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

            location ~ \.php$



            fastcgi_index index.php;

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

            fastcgi_param SCRIPT_FILENAME /home/username/public_html/$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.


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.




Please enter an answer

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] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct