您的当前位置: 首页>>漳平商学院>>漳平SEO优化

漳平301重定向:LNMP下实现301重定向的办法

浏览量(77631) 时间:2020-09-11

LNMP下实现301重定向办法一:编辑伪静态.htaccess文件

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

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


LNMP下实现301重定向办法二:打开/usr/local/nginx/conf/vhost下相应的.conf文件,原代码如下:

server
{
listen 80;
server_name www.szcew.com szcew.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.szcew.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.szcew.com szcew.com; 的szcew.com删除掉,然后在代码的最下面再加上一个server段:

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

最后得到的完整代码是:

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

LNMP下实现301重定向办法三:LNMP推荐的方法 ,这种方法效率高,


LNMP下的Nginx如果想将域名szcew.com 301重定向到www.szcew.com,同时www.szcew.com已经通过/root/vhost.sh添加上,可以按如下步骤修改,使用命令编辑器vi、nano或winscp图形管理软件编辑对应的虚拟主机,一般虚拟主机配置文件位于:/usr/local/nginx/conf/vhost/域名.conf ,如果添加的域名是www.vpsdx.com,则配置文件是/usr/local/nginx/conf/vhost/www.szcew.com.conf ,在配置文件最后面加上如下代码:

省略www.szcew.com虚拟主机server配置
server {
listen 80;
server_name szcew.com;
return 301 https://www.szcew.com$request_uri;
}

如果想将域名www.szcew.com 301重定向到szcew.com,同时szcew.com已经通过/root/vhost.sh添加上,则编辑对应的虚拟主机,一般虚拟主机配置文件位于:/usr/local/nginx/conf/vhost/szcew.com ,如果添加的域名是www.vpsdx.com,则配置文件是/usr/local/nginx/conf/vhost/www.szcew.com,在配置文件最后面加上如下代码:

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

得到的完整代码如下:

server
 {
 listen 80;
 #listen [::]:80;
 server_name szcew.com; //此处把www.szcew.com域名删除//
 index index.html index.htm index.php default.html default.htm default.php;
 root /home/wwwroot/szcew.com;

 include other.conf;
 #error_page 404 /404.html;

 # Deny access to PHP files in specific directory
 #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

 include enable-php.conf;

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

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

 location ~ /.well-known {
 allow all;
 }

 location ~ /\.
 {
 deny all;
 }

 access_log /home/wwwlogs/wget.ee.log;
 }
//下面为新增的代码 //
server {
listen 80;
server_name www.szcew.com;
return 301 http://szcew.com$request_uri;
}

添加完成后保存,执行:/etc/init.d/nginx restart 重启nginx,使其生效。

如果是想让http强制跳转到https,把里面的http换成https就行。
例:

server {
listen 443 ssl;
server_name www.szcew.com;
省略其他配置
}
server {
listen 80;
server_name www.szcew.com;
return 301 https://www.szcew.com$request_uri;
}