下面是编程之家 jb51.cc 通过网络收集整理的代码片段。编程之家小编现在分享给大家,也给大家做个参考。
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 记录解析。
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。