2016-01-22 147 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