Report this

What is the reason for this report?

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

Posted on May 11, 2014

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.



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!

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.

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.

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?

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.