0

I am very new to nginx. I was using Apache previously and was using htaccess to redirect root to another folder. Now migrated to nginx. Here is four things I want to achieve

  1. Redirect http to https e.g. http://www.example.com -> https://www.example.com
  2. Then redirect root to another folder but URL must be rewritten as example.com not example.com/blog
  3. All files in php should show as html in url e.g. example.com/contact.php -> example.com/contact.html
  4. example.com/page.php?content=file -> example.com/file I found this code to redirect but don't know where to insert this code nginx.conf or any other file?

    server{

    location = / { return 301 https://www.example.com/blog; } 

    }

Also please suggest me if these changes are made in nginx.conf file or /etc/nginx/sites-available/example.com file.

4
  • why do you want to redirect to another document root? why not just set the correct one in the first place. You will probably get more help if you show the full config file - or at least the sections of the config you are talking about Commented Jan 15, 2016 at 9:10
  • we have some SEO issues so we can't remove current files from the root. Commented Jan 15, 2016 at 9:12
  • are you talking about the root URL or the document root? Commented Jan 15, 2016 at 9:13
  • the root url. like the example I have mentioned above Commented Jan 15, 2016 at 9:24

1 Answer 1

1

To redirect HTTP to HTTPS traffic you can create another server block to match the incoming HTTP and domain then rewrite to your HTTPS server block.
Which file do you put this in? Both /etc/nginx/nginx.conf and /etc/nginx/sites-available/example.com should get read (unless you changed config) so it shouldn't matter, but I personally put these configs in /etc/nginx/sites-available/example.com because I consider it part of the same domain.

file: /etc/nginx/sites-available/example.com

 server { listen 80; server_name www.example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name www.example.com; ... # your location blocks and redirects here } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank a lot friend. Can you provide the URL rewrite rule and redirect block too? is it same I mentioned above
What about $scheme://domain.com$request_uri; (seen on serverfault)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.