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
總結
以上是生活随笔為你收集整理的LNMP下实现301重定向的三种办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lnmp环境下MYSQL启动失败,提示C
- 下一篇: html5新增标签总结