WenRou's Blog

新浪微薄腾讯微薄

最新碎语:测试图片碎语哦

您的位置:WenRou's Blog >lnamp> nginx 301 域名重定向

nginx 301 域名重定向

以下是以discuz论坛为例

server
{
listen 80;
server_name wenrouge.com ;

if ($host != 'www.wenrouge.com' ) {
rewrite ^/(.*)$ http://www.wenrouge.com/$1 permanent;
}

index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot;

include discuz.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 wenrouge.com ;
if ($host != 'www.wenrouge.com' ) {
rewrite ^/(.*)$ https://www.wenrouge.com/$1 permanent;
}
该代码的意思 输入的域名如果不是wenrouge.com就直接跳转到www.wenrouge.com 也可以这样写
server_name wenrouge.com ;
if ($host = 'wenrouge.com' ) {
rewrite ^/(.*)$ https://www.wenrouge.com/$1 permanent;
}
该代码的意思 输入的域名如果为 wenrouge.com就直接跳转到www.wenrouge.com


---

转载请注明本文标题和链接:《nginx 301 域名重定向

发表评论

70 + 5 =
路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交