[英]nginx with php and rewrite rule
我想用php和一個簡單的重寫規則設置我的nginx服務器。 我試圖制定一個重寫規則,將指向不存在的文件或目錄的所有URL都重寫為/index.php?q=。 我創建了這個nginx.conf:
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;
}
}
}
這可行,但並非在每種情況下都可行。 它像tihs一樣完美地重寫了utls:
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 url重寫為/index.php,而不是我想要在論壇目錄中執行index.php。
我該如何解決這兩個問題?
謝謝!
您的配置絕對錯誤,請用單行替換位置/內容:
try_files $uri $uri/ /index.php?q=$uri;
如果要重定向到特定目錄中的索引文件,則需要將重定向的格式設置為類似以下格式
rewrite ^/((.*/)*)(.*)$ /$1index.php?q=$3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.