nginx url重写(无重定向)

时间:2016-01-22 15:39:53

标签: url nginx url-rewriting rewrite

您好我想将nginx重写为fllowing

http://my-domain/Canada

http://my-domain/rates/callrates.php?c=Canada

用户会将网址视为http://my-domain/Canada 并且php将执行以下URL中的页面

http://my-domain/rates/callrates.php?c=Canada

如何在nginx.conf中启用它?

server 
{
    listen 80;
    server_name  my-domain.com;
    access_log /home/www/my-domain/logs/access.log;
    error_log /home/www/my-domain/logs/error.log;
    root /home/www/my-domain/public_html;
    location / {
        index index.php login.php;
    try_files $uri $uri/ $uri.php?$query_string @extensionless-php;
    error_page  404  /404.php;      
    }
location ~* ^/(?<country>\w+)$ {
    rewrite ^ /testring/callrates.php?c=$country last;
}
 location ~ \.html$ 
 {
    if (!-f $request_filename) 
    {
        rewrite ^(.*)\.html$ $1.php last;
    }
}
location ~ .*\.php$  
    {   include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/www/r2wconnect.com/public_html$fastcgi_script_name;
    }
location @extensionless-php 
{
   rewrite ^(.*)$ $1.php last;
}
}

server { listen 80; server_name my-domain.com; access_log /home/www/my-domain/logs/access.log; error_log /home/www/my-domain/logs/error.log; root /home/www/my-domain/public_html; location / { index index.php login.php; try_files $uri $uri/ $uri.php?$query_string @extensionless-php; error_page 404 /404.php; } location ~* ^/(?<country>\w+)$ { rewrite ^ /testring/callrates.php?c=$country last; } location ~ \.html$ { if (!-f $request_filename) { rewrite ^(.*)\.html$ $1.php last; } } location ~ .*\.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/r2wconnect.com/public_html$fastcgi_script_name; } location @extensionless-php { rewrite ^(.*)$ $1.php last; } }

1 个答案:

答案 0 :(得分:1)

假设您已经有一个可用的PHP配置...

如果您的域名不执行任何其他操作,则默认操作可能是使用以下命令调用脚本:

location / {
    try_files $uri /rates/callrates.php?c=$uri;
}

以上有副作用留在前导/

如果您想使规则更具体,可以使用正则表达式(也正确提取国家/地区名称)来保护它:

location ~* ^/(?<country>\w+)$ {
    rewrite ^ /rates/callrates.php?q=$country last;
}