您的位置 首页 VPS教程

LNMP环境301重定向的有效方法

本文标签: 301重定向 , LNMP 因为前几天买了W2的VPS,因为还在找对方解决一些事情,就装了下LNMP环境什么都没放,今天闲来无事,暂时放一个美女图片在上面,放了之后发现…

本文标签: 301重定向 , LNMP

因为前几天买了W2的VPS,因为还在找对方解决一些事情,就装了下LNMP环境什么都没放,今天闲来无事,暂时放一个美女图片在上面,放了之后发现,我通过./vhost.sh建立的虚拟主机域名设置www.01mm.net,当打开不带www的01mm.net的时候,竟然是LNMP主目录的页面。

怎么让01mm.net自动转向www.01mm.net呢?这就是301重定向的问题了。

首先打开/usr/local/nginx/conf/vhost/www.01mm.net.conf文件,原代码如下:

server
{
listen       80;
server_name www.01mm.net;
index index.html index.htm index.php default.html default.htm default.php;
root  /home/wwwroot/www.01mm.net;

include other.conf;
location ~ .*\.(php|php5)?$
{
fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
}

location ~ .*\.(js|css)?$
{
expires      12h;
}

access_log off;
}

我在这段代码最后加入如下代码:

server {
server_name 01mm.net;
rewrite ^(.*) http://www.01mm.net$1 permanent;
}

大家注意,如果原代码server_name后面有2个地址,包括了不带www的地址,请先删除之!

另外,修改完成后,请重启LNMP。

/root/lnmp stop

/root/lnmp start

到这里,301重定向就完成了。现在不输入www的地址,也能自动转到www了。来个美女图片站的图吧:

301

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/2408.html

为您推荐

返回顶部