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

RewriteEngine on
RewriteCond %{http_<a href="https://www.yehes.com/tag/hos" title="【查看含有[hos]标签的文章】" target="_blank">hos</a>t} ^yehes.com [NC]
RewriteRule ^(.*)$ https://www.yehes.com/$1 [L,R=301]

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

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

server
{
listen 80;
server_<a href="https://www.yehes.com/tag/name" title="【查看含有[name]标签的文章】" target="_blank">name</a> www.yehes.com yehes.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.yehes.com; include none.conf;
locat<a href="https://www.yehes.com/tag/ion" title="【查看含有[ion]标签的文章】" target="_blank">ion</a> ~ .*\.(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.yehes.com yehes.com; 的yehes.com删除掉,然后在代码的最下面再加上一个server段:

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

最后得到的完整代码是:

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

LNMP下实现301重定向办法三:LNMP推荐的方法 ,这种方法效率高,目前VPS大学采用的此方法。

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

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

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

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

得到的完整代码如下:

server
{
listen 80;
#listen [::]:80;
server_name yehes.com; //此处把www.yehes.com域名删除//
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/yehes.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.yehes.com;
return 301 http://yehes.com$request_uri;
}

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

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

server {
listen 443 <a href="https://www.yehes.com/tag/ssl" title="【查看含有[ssl]标签的文章】" target="_blank">ssl</a>;
server_name www.yehes.com;

省略其他配置

}
server {
listen 80;
server_name www.yehes.com;
return 301 https://www.yehes.com$request_uri;
}
夜河资源网提供的所有内容仅供学习与交流。通过使用本站内容随之而来的风险以及法律责任与本站无关,所承担的法律责任由使用者承担。
一、如果您发现本站侵害了相关版权,请附上本站侵权链接和您的版权证明一并发送至邮箱:yehes#qq.com(#替换为@)我们将会在五天内处理并断开该文章下载地址。
二、本站所有资源来自互联网整理收集,全部内容采用撰写共用版权协议,要求署名、非商业用途和相同方式共享,如转载请也遵循撰写共用协议。
三、根据署名-非商业性使用-相同方式共享 (by-nc-sa) 许可协议规定,只要他人在以原作品为基础创作的新作品上适用同一类型的许可协议,并且在新作品发布的显著位置,注明原作者的姓名、来源及其采用的知识共享协议,与该作品在本网站的原发地址建立链接,他人就可基于非商业目的对原作品重新编排、修改、节选或者本人的作品为基础进行创作和发布。
四、基于原作品创作的所有新作品都要适用同一类型的许可协议,因此适用该项协议, 对任何以他人原作为基础创作的作品自然同样都不得商业性用途。
五、根据二〇〇二年一月一日《计算机软件保护条例》规定:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可不经软件著作权人许可,无需向其支付报酬!
六、鉴此,也望大家按此说明转载和分享资源!本站提供的所有信息、教程、软件版权归原公司所有,仅供日常使用,不得用于任何商业用途,下载试用后请24小时内删除,因下载本站资源造成的损失,全部由使用者本人承担!