1. 程式人生 > >Nginx 重定向 偽靜態 rewrite index.php

Nginx 重定向 偽靜態 rewrite index.php

分類 lib aaa p s ons 靜態 sta ces .html

參考https://www.kancloud.cn/manual/thinkphp5/177576

thinkphp入口文件同目錄下添加。把下面的內容保存為.htaccess文件

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>

服務器

vim /usr/local/nginx/conf/vhost/www.phpaaa.com.conf

在http{ server{ }}裏寫代碼,在原來那些location附近寫

location / {

if (!-e $request_filename){

rewrite ^/(.*)$ /index.php/$1 last;

}

}

重啟

lnmp start

使用情境:我想輸入www.abc.com/a/1後,實際上是跳轉到www.abc.com/index.PHP/a/1


配置Nginx.conf在你的虛擬主機下添加:
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1 last;
}
}


如果你的項目入口文件在一個子目錄內,則:
location /目錄/ {
if (!-e $request_filename){
rewrite ^/目錄/(.*)$ /目錄/index.php/$1 last;
}
}

Nginx主配置(默認虛擬主機)文件:/usr/local/nginx/conf/nginx.conf

添加的虛擬主機配置文件:/usr/local/nginx/conf/vhost/域名.conf

http://blog.csdn.net/beyondlpf/article/details/8261657

http://www.cnblogs.com/300js/p/6484642.html

 仔細觀察 rewrite ^(.*)/t(\d+)\.html$ $1/index.php?t=3 last;其實感覺nginx的偽靜態規則蠻好寫的。就是用正則的基礎上,一個rewrite來聲明,然後^是偽靜態規則開頭,(.*)匹配任意字符,這裏匹配的就是域名了,t就是你在這裏想加的字符,如你可以加apple、orange這樣的分類名了,(\d+)匹配的是數字,\.html匹配的是後綴,$就是正則匹配的結束。後面半部分就是要改寫的url了,用$1打頭,表示域名,/index.php?t=3就是要改寫的URL,用last;結束即可。

來源: http://www.cnblogs.com/thinksasa/archive/2012/12/16/2820130.html

rewrite ^(.*)/t(\d+)\.html$ $1/index.php?t=3 last;

rewrite 輸入的url 要跳轉的url last;

==========================

(-d $request_filename)它有個條件是必需為真實目錄,而我的rewrite不是的,所以沒有效果

if (-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

=====================

文件和目錄不存在的時候重定向:

復制代碼代碼如下:

if (!-e $request_filename) {

proxy_pass http://127.0.0.1;

}

=====

Nginx 重定向 偽靜態 rewrite index.php