redirect everything under one path to another domain – nginx

I have a problem with rewrite/redirect urls.

I had site website.com/news .. where in /news were all articles.

Read More

I did new site articles.website.com and I want to redirect all link under /news to redirect on articles.website.com.

So in example:

website.com/news/awesome-article redirect/rewrite on articles.website.com/awesome-article .. is that possible with nginx? Thank you.

I used

     location ~ ^/news/(.*) {
  return 301 http://www.articles.website.com/$1;
}

But it`s still on new url redirection on articles.website.com/news/awesome-article .. but I want it withnout /news.

My vhost for site I want to redirect (website.com)

   server {
    listen   80; ## listen for ipv4; this line is default and implied

    root /var/www/vhosts/website.com;
    index index.html index.htm index.php;

    error_log /var/log/nginx/website.com.error.log;
    access_log /var/log/nginx/website.com.access.log;

    server_name dev.website.com website.com www.website.com;

    location / {
                try_files       $uri $uri/ /index.php?$args;
        }

#   location = /news {
  #return 301 http://articles.website.com$1;
#}

#   location = /news/ {
 # return 301 http://articles.website.com/$1;
#}

#   location = /news/wp-login.php {
 # return 301 http://articles.website.com/wp-login.php$1;
#}

      location ~ ^/news/(.*) {
return 301 http://articles.website.com/??$1;
}


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
    #   fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #   deny all;
    #}
}

Related posts

1 comment

  1. Sorry for bothering. I don`t know why, but nginx did not want to accept that redirect. After night, I double check that rule, and it wokrs.

Comments are closed.