Question

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

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 (ahemahem.com) at digitalocean and I’ve set my main domain too. I’ve installed Wordpress on ahemahem.com using Easy Engine which works on Nginx.


Submit an answer

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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

@talwarsaksham: The location block needs to be inside of the server block. What were the actual errors you were seeing?

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/site.com/nginx.conf or etc/nginx/nginx.conf? I tried on both. In site.com it doesnt work where as in other he server crashes. <br> <br>I’ve pointed media.ahemahem.com to my droplet ip. Do I need to do anything else?

Google is an amazing thing. It finds you things like http://winginx.com/en/htaccess that turn your .htaccess rewrites into nginx configuration that looks like this: <br> <br># nginx configuration <br>location / { <br>if ($http_host ~ “^media.ahemahem.com$”){ <br>rewrite ^(.*)$ /“http:/www.ahemahem.com/wp-content/uploads/$1” redirect; <br>} <br>} <br> <br>It goes in your config file.