前端运维

WordPress程序伪静态规则(Nginx/Apache)及二级目录规则

在众多CMS程序中,我们使用WORDPRESS还是比较多的,不仅仅是安全度较好,二来在于插件和主题很多,即便对于不会建站技术的用户也很简单的就可以搭建属于自己的网站项目。对于网站我们肯定是需要让有用户访问量才能创造价值,比如收益比如得到用户的赞许都是我们做网站的用户希望看到的。早年我们可能较多的会使用动态目录页面,但是对于搜索引擎不是 很好,我们最好是使用伪静态或者生成静态页面。

在WORDPRESS程序应用的时候伪静态规则是需要根据我们服务器环境来设定的,比如NGINX或者APACHE是我们常用的规则,这里我将两个规则对应的规则内容代码整理出来。

第一、APACHE环境

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

这个上面代码是适合APACHE环境的,我们只要根目录有一个.htaccess文件,然后丢上上面的文件就可以。

第二、Nginx环境

    location / {
    if (-f $request_filename/index.html){
                    rewrite (.*) $1/index.html break;
            }
    if (-f $request_filename/index.php){
                    rewrite (.*) $1/index.php;
            }
    if (!-f $request_filename){
                    rewrite (.*) /index.php;
            }
    }

一般虚拟主机都是APACHE环境的,如果是VPS,我们很多都是用的NGINX,这里需要在对应站点的NGINX环境中应用到这个规则,比如/usr/local/nginx/conf/wordpress.conf

设定到这个文件,然后在站点配置文件中引用。

/usr/local/nginx/conf/vhost/hostusvps.com.conf

server {
listen 80;
server_name hostusvps.com www.hostusvps.com;
access_log /home/wwwlogs/hostusvps.com_nginx.log combined;
index index.html index.htm index.php;
include wordpress.conf;
root /home/wwwroot/hostusvps.com;

location ~ .*\.(php|php5)?$ {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }
location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}

上面脚本红色部分就是引用到的伪静态规则。

第三、二级目录规则

如果我们在站点二级目录文件夹单独用一个WP程序,那规则就不同了。

    location /hostusvps.com/ {
    index index.html index.php;
    if (-f $request_filename/index.html){
    rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename/index.php){
    rewrite (.*) $1/index.php;
    }
    if (!-f $request_filename){
    rewrite (.*) /hostusvps.com/index.php;
    }
    }

这里我们对应的是hostusvps.com二级目录文件夹,如果是需要用其他文件夹那就换一个对应名称。修改和添加到根目录的HTACCESS文件中就可以了。

posted on 2016-01-26 18:09  前端运维  阅读(3657)  评论(0编辑  收藏  举报

导航