LNMP下实现301重定向的三种办法

LNMP下实现301重定向的办法。
方法一:

编辑伪静态.htaccess文件

RewriteEngine on
RewriteCond %{http_host} ^fengjunzi.com [NC]
RewriteRule ^(.*)$ https://www.fengjunzi.com/$1 [L,R=301]

这种方法没有写permanent,没有的话也能重定向,但属于302重定向!

方法二:

打开/usr/local/nginx/conf/vhost下相应的.conf文件,原代码如下:

server
{
listen 80;
server_name www.fengjunzi.com fengjunzi.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.fengjunzi.com fengjunzi.com; 的fengjunzi.com删除掉, 然后在代码的最下面再加上一个server段:

server {
server_name myhuabao.com;
rewrite ^(.*) https://www.fengjunzi.com$1 permanent;
}

最后得到的完整代码是:

server
{
listen 80;
server_name www.fengjunzi.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.fengjunzi.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 fengjunzi.com;
rewrite ^(.*) https://www.fengjunzi.com$1 permanent;
}

方法三:

具体这种方法效率高,目前我们采用的此方法。 例如虚拟主机配置文件位于:/usr/local/nginx/conf/vhost/域名.conf ,如添加的域名是www.fengjunzi.com则配置文件是/usr/local/nginx/conf/vhost/www.fengjunzi.com.conf 在配置文件最后面加上如下代码:

server {
listen 80;
server_name fengjunzi.com;
return 301 https://www.fengjunzi.com$request_uri;
}

这样用户打开fengjunzi.com时候就会转到www.fengjunzi.com去了,注意,fengjunzi.com虽然用了301重定向,但还是要做A记录解析。

本文转自:https://www.22vd.com/5239.html

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注