简体   繁体   中英

nginx rewrite sub url

How can I solve this problem: I want to set up nginx conf file to meet below criteria:

http://www.example.com/site1/article/index.php?q=hello-world -> http://www.example.com/site1/article/hello-world

httb://www.example.com/site2/article/index.php?q=goodbye-world -> httb://www.example.com/site2/article/goodbye-world

httb://www.example.com/site3/article/index.php?q=open-new-world -> httb://www.example.com/site3/article/open-new-world

There are multiple sites after example.com, I want to make the url look clean by using nginx configuration.

But my below configuration doesn't work. Someone help me?

server {
listen 80;
listen [::]:80;

root /var/www/example.com/public_html;
index index.php index.html index.htm;

server_name www.example.com;     
location ~ /article/ {
    try_files $uri /site1/article/index.php?q=$1;

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

}

You would like the client to present a URL like /xxx/article/yyy which is then internally rewritten to /xxx/article/index.php?q=yyy .

You need to capture the components of the source URI in order to use them later. You have a $1 in your question, but you are missing the expression to actually give it a value. With the minimum number of changes, this works:

location ~ ^(.*/article/)(.*)$ {
    try_files $uri $1index.php?q=$2;
    location ~ \.php$ { ... }
}

However, you do not need to use a nested location for PHP, as long as the PHP regex location appears above the other regex location, it will process all php files. For example:

location ~ \.php$ { ... }

location ~ ^(.*/article/)(.*)$ {
    try_files $uri $1index.php?q=$2;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM