Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/;
}
like image 528
Charles Avatar asked Oct 30 '22 15:10

Charles


1 Answers

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.

like image 74
Arman Ordookhani Avatar answered Nov 15 '22 05:11

Arman Ordookhani