LNMP下實現301重定向辦法一:編輯偽靜態(tài).htaccess文件
RewriteEngine on
RewriteCond %{http_host} ^szcew.com [NC]
RewriteRule ^(.*)$ http://www.ultra-do.com/$1 [L,R=301]
這種方法沒有寫permanent,沒有的話也能重定向,但屬于302重定向!
LNMP下實現301重定向辦法二:打開/usr/local/nginx/conf/vhost下相應的.conf文件,原代碼如下:
server
{
listen 80;
server_name www.ultra-do.com szcew.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.ultra-do.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.ultra-do.com szcew.com; 的szcew.com刪除掉,然后在代碼的最下面再加上一個server段:
server {
server_name vpsdx.com;
rewrite ^(.*) http://www.ultra-do.com$1 permanent;
}
最后得到的完整代碼是:
server
{
listen 80;
server_name www.ultra-do.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.ultra-do.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 ^(.*) http://www.ultra-do.com$1 permanent;
}
LNMP下實現301重定向辦法三:LNMP推薦的方法 ,這種方法效率高,
LNMP下的Nginx如果想將域名szcew.com 301重定向到www.ultra-do.com,同時www.ultra-do.com已經通過/root/vhost.sh添加上,可以按如下步驟修改,使用命令編輯器vi、nano或winscp圖形管理軟件編輯對應的虛擬主機,一般虛擬主機配置文件位于:/usr/local/nginx/conf/vhost/域名.conf ,如果添加的域名是www.vpsdx.com,則配置文件是/usr/local/nginx/conf/vhost/www.ultra-do.com.conf ,在配置文件最后面加上如下代碼:
省略www.ultra-do.com虛擬主機server配置
server {
listen 80;
server_name szcew.com;
return 301 http://www.ultra-do.com$request_uri;
}
如果想將域名www.ultra-do.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.ultra-do.com,在配置文件最后面加上如下代碼:
server {
listen 80;
server_name www.ultra-do.com;
return 301 http://szcew.com$request_uri;
}
得到的完整代碼如下:
server
{
listen 80;
#listen [::]:80;
server_name szcew.com; //此處把www.ultra-do.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.ultra-do.com;
return 301 http://szcew.com$request_uri;
}
添加完成后保存,執(zhí)行:/etc/init.d/nginx restart 重啟nginx,使其生效。
如果是想讓http強制跳轉到https,把里面的http換成https就行。
例:
server {
listen 443 ssl;
server_name www.ultra-do.com;
省略其他配置
}
server {
listen 80;
server_name www.ultra-do.com;
return 301 http://www.ultra-do.com$request_uri;
}