Skip to main content

Forums

This topic contains 10 replies, has 2 voices, and was last updated by  chrisburton 4 years, 9 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #186003

    chrisburton
    Participant

    I can’t seem to find a tutorial that works.

    I’m trying to redirect an old domain and anything typed after / to redirect to a new domain.

    For example ( => equals redirect ):

    domain.net => domain.me
    domain.net/something => domain.me/something

    #186004

    Alen
    Participant

    Try

    
    location / {
        rewrite ^(.*)$ http://www.domain.me/$1 redirect;
    }
    
    #186006

    chrisburton
    Participant

    This reply has been reported for inappropriate content.

    @AlenAbdula wouldn’t I need to create a new server block with server_name domain.net to use that code?

    #186007

    Alen
    Participant

    Nginx config file domain.net.conf in /etc/nginx/sites-available, don’t forget to system link it to sites-enabled folder and reboot Nginx.

    
    server {
    
            listen 80;
            server_name domain.net;
            root /folder/on/the/server/www;
    
            location / {
                rewrite ^(.*)$ http://www.domain.me/$1 redirect;
            }
    
            location ~ /\.ht {
                deny all;
            }
    }
    
    #186008

    chrisburton
    Participant

    This reply has been reported for inappropriate content.

    @AlenAbdula One sec. Just so we’re on the same page. The primary setup is under domain.me. So server_name by default is domain.me.

    Nginx config file domain.net.conf in /etc/nginx/sites-available

    Are you suggesting I need to make a config file inside of /etc/nginx/sites-available and name it domain.net.conf?

    don’t forget to system link it to sites-enabled folder

    How would I go about doing that? Of course I’ll search as soon as I submit this.

    #186009

    Alen
    Participant

    Each of the domains that your server is responding to should have its own config file. So if you are responding to domain.net and domain.me each needs seperate config files. You would place these in the sites-available folder. Then you would system link them, similar to what a2ensite does for Apache, to the sites-enabled.

    sudo ln -s /etc/nginx/sites-available/domain.me.conf /etc/nginx/sites-enabled/domain.me.conf

    sudo service nginx reload

    #186010

    chrisburton
    Participant

    This reply has been reported for inappropriate content.

    @AlenAbdula

    Oh ok! So /sites-enabled/default and sites-available/default should be changed to my domain, correct?

    #186011

    Alen
    Participant

    The names are not really important. That’s more for our conviniance. Server name and document root we really care about. Nginx just loads the config files. You can have eveyrthing in one file if you wanted to.

    So I would leave the default as is. If that’s your main website, then just copy that config file, name it old.site.config if you want, then just add the code above, responding to appropriate domain. It’s really up to you how you want to structure it.

    #186012

    Alen
    Participant

    EDIT: Linked to wrong video, this one is using wildcard subdomains. I’ll leave it here if someone find it useful. Let me go find the correct one.

    Watch this and you’ll see the process: https://serversforhackers.com/editions/2014/03/25/nginx/

    #186013

    chrisburton
    Participant

    This reply has been reported for inappropriate content.

    So I would leave the default as is. If that’s your main website, then just copy that config file, name it old.site.config if you want, then just add the code above, responding to appropriate domain. It’s really up to you how you want to structure it.

    If I sync old-site.config with the code you applied above, I get an error when I try to reload Nginx.

    Visual of my directory: http://cl.ly/XyXn

    #186014

    chrisburton
    Participant

    This reply has been reported for inappropriate content.

    Ok I got the above fixed. It was due to a long domain name so I had to add something extra into nginx.conf. However, I noticed that on redirect, an extra slash gets added to the URL?

    For example,

    domain.net => domain.me//

    Edit: Removing the forward slash from the rewrite solves it.

    Thanks a lot, Alen!!

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

icon-link icon-logo-star icon-search icon-star