2012-07-24 63 views
0

我想用php設置我的nginx服務器,並且有一個簡單的重寫規則。我試圖制定一個重寫規則,即任何指向非現有文件或目錄的URL都會重寫爲/index.php?q=。我創建了這個nginx.conf:nginx用php和重寫規則

events { 
} 

http { 
    include fastcgi.conf; 
    server { 
    listen   80; 
    server_name  www.example.com; 
    index   index.php; 
    root   /var/www/html/www.example.com; 
    location/{ 
     index  index.php; 
     if (!-f $request_filename) { 
     rewrite ^/(.*)$ /index.php?q=$1 last; 
     } 
     if (!-d $request_filename) { 
     rewrite ^/(.*)$ /index.php?r=$1 last; 
     } 
    } 
    location ~ \.php { 
     try_files $uri =404; 
     fastcgi_pass 127.0.0.1:9999; 
    } 
    } 
} 

這是有效的,但不是在任何情況下。它完美地改寫UTLS像tihs:

http://www.example.com/111/222/333

但兩個案例沒有工作,因爲我想:

http://www.example.com/111/222/333.php

下降404錯誤。

www.example.com/forum/index.php存在,所以www.example.com/forum/index.php在論壇目錄中執行index.php,這很好。但www.example.com/forum網址會重寫爲/index.php,不會在論壇目錄中執行index.php。

我該如何解決這兩個問題?

謝謝!

回答

1

你的配置是絕對錯誤的,更換位置/內容與單行:

try_files $uri $uri/ /index.php?q=$uri; 
0

您需要格式化您重定向到是這樣的,如果你想要一個特定的目錄中重定向到一個索引文件

rewrite ^/((.*/)*)(.*)$ /$1index.php?q=$3