Jerry Jones
Jerry Jones

Reputation: 796

Redirect root to another folder in nginx server

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.

Upvotes: 0

Views: 3443

Answers (1)

the_velour_fog
the_velour_fog

Reputation: 2184

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
 }

Upvotes: 1

Related Questions