2016-01-22 141 views
1

您好我想打一个nginx的重写作为fllowingnginx的URL重写(无重定向)

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; 
} 
} 

+0

我看不出新的位置块符' .css'和'.js',除非它们的网址没有扩展名,因此看起来像国家/地区名称URL。 –

+0

嗨,我设法解决它。这是由于html标头中的基址。现在所有的工作都完美谢谢你这么多的时间 – Vigikaran

回答

1

假设你已经有一个工作的PHP配置...

如果您的域名不说别的,默认操作可能是与调用脚本:

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

以上具有在领先的/中留下的副作用。

如果你想使规则更具体的,你可以用正则表达式保护它(这也正确提取国家名):

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

嗨,我TREID左右,但它给404错误的css,js文件 – Vigikaran

+0

如果您编辑您的问题,并添加您现有的'server'块,我们可以在上面集成到它提供咨询意见。 –

+0

我已经用我所有的方块更新了这个问题 – Vigikaran