Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX Redirect or Alias?

Tags:

nginx

I want to do a redirect in order to give a cleaner url for my users.

I want to change:

http://mydomain.com/main/username/profile

To:

http://mydomain.com/username/profile

Would I do this with a rewrite or an alian and how?

like image 459
ChrisInCambo Avatar asked Jul 18 '26 16:07

ChrisInCambo


1 Answers

First you have to understand what the difference between a redirect and an alias is.

Redirect

A redirect will send a user who requests /main/username/profile to /username/profile. The URL will change in the browser. This is particularly important if the URL is accessible by search engines, because they would otherwise index the same page twice (duplicated content).

If you decide to use a redirect you should be sure, that your URLs stay that way. The reason for this is Cool URIs don't change.

Example for a redirect:

server {
    # ...
    location / {
        # ...
        location ~ /main/([a-zA-Z0-9]+)/profile$ {
            # SEO effective redirect
            return 301 /$1/profile;
        }
        # ...
    }
}
  • nginx documentation: return
  • nginx wiki: return

Alias

An alias is used to tell nginx that that a requested file is not mapped by the URL on the filesystem and that it should have a look elsewhere. The following example is from the nginx wiki:

root /var/www;
location  /i/ {
    alias  /spool/w3/images/;
}

A request for /i/empty.gif will not map to /var/www/i/empty.gif. Instead it will be matched to /spool/w3/images/empty.gif.

  • nginx documentation: alias
  • nginx wiki: alias
like image 80
Fleshgrinder Avatar answered Jul 21 '26 06:07

Fleshgrinder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!