Nginx 重定向 伪静态 rewrite index.php

参考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;

}

=====

posted @ 2017-09-03 01:37  wujunbin  阅读(6036)  评论(0编辑  收藏  举报