Charles
Charles

Reputation: 83

Nginx Rewrite Paths to Base URL

I am trying to configure Nginx so that all requests to http://domain.com/path are rewritten to http://domain.com/.

I do not want a redirect, because I want the URL to still show the original path.

Example Rewrites:

http://domain.com/path/index.php         -> http://domain.com/index.php
http://domain.com/path/category/books    -> http://domain.com/category/books
http://domain.com/path/books.php?q=harry -> http://domain.com/books.php?q=harry

I tried alias and root but I could not get them to work.

location /path
{
    root /var/www/html/;
}

location /path
{
    alias /var/www/html/;
}

Upvotes: 6

Views: 14419

Answers (1)

Arman Ordookhani
Arman Ordookhani

Reputation: 6537

root and alias are meant to serve files from a specific directory not rewriting URL. You should use rewrite.

server {
    rewrite ^/path(/.*)$ $1 last;

    # Your location blocks go here.
}

Read official docs for more info.

Upvotes: 2

Related Questions