LNMP下实现301重定向的办法
以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考 LNMP 下实现 301 重定向的办法。 方法一:编辑伪静态.htaccess 文件 RewriteEngine on RewriteCond %{http_host} ^22vd.com [NC] RewriteRule ^(.*)$ https://www.22vd.com/$1 [L,R=301] 这种方法没有写 permanent,没有的话也能重定向,但属于 302 重定向! 方法二:打开/usr/local/nginx/conf/vhost 下相应的.conf 文件,原代码如下: server { listen 80; server_name www.22vd.com 22vd.com; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/www.myhuabao.com; include none.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_name www.22vd.com 22vd.com; 的 22vd.com 删除掉, 然后在代码的最下面再加上一个 server 段: server { server_name myhuabao.com; rewrite ^(.*) https://www.22vd.com$1 permanent; } 最后得到的完整代码是: server { listen 80; server_name www.22vd.com; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/www.22vd.com; include none.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 22vd.com; rewrite ^(.*) https://www.22vd.com$1 permanent; } 方法三:具体这种方法效率高,目前我们采用的此方法。 例如虚拟主机配置文件位于:/usr/local/nginx/conf/vhost/域名.conf ,如添加的域名是 www.22vd.com 则配置文件是/usr/local/nginx/conf/vhost/www.22vd.com.conf 在配置文件最后面加上如下代码: server { listen 80; server_name 22vd.com; return 301 https://www.22vd.com$request_uri; } 这样用户打开 22vd.com 时候就会转到 www.22vd.com 去了,注意,22vd.com 虽然用了 301 重定向,但还是要做 A 记录解析。 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |