How to redirect a some files from a subdomain to corresponding files of a domain in Nginx?

May 11, 2014 1.8k views
I want to 301 redirect a bunch of files from a subdomain to another domain. This is my .htaccess file, can someone supply the code that works with nginx RewriteEngine on RewriteCond %{HTTP_HOST} ^media\.ahemahem\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.media\.ahemahem\.com$ RewriteRule ^(.*)$ "http\:\/\/www\.ahemahem\.com\/wp\-content\/uploads\/$1" [R=301,L] and where should I add this code? I've set up a A record (media) for the domain ( at digitalocean and I've set my main domain too. I've installed Wordpress on using Easy Engine which works on Nginx.
3 Answers
Google is an amazing thing. It finds you things like that turn your .htaccess rewrites into nginx configuration that looks like this:

# nginx configuration
location / {
if ($http_host ~ "^media\.ahemahem\.com$"){
rewrite ^(.*)$ /"http\:\/www\.ahemahem\.com\/wp\-content\/uploads\/$1" redirect;

It goes in your config file.
Yeah, already tried that stuff which didn't work and resulted in my server failing and all my sites went down. So I asked here. It was supposed to go in the nginx.conf files right? In the main /var/www/ or etc/nginx/nginx.conf? I tried on both. In it doesnt work where as in other he server crashes.

I've pointed to my droplet ip. Do I need to do anything else?
@talwarsaksham: The location block needs to be inside of the server block. What were the actual errors you were seeing?
Have another answer? Share your knowledge.