您的位置:首页 > 运维架构 > Nginx

Nginx和Apache配置301跳转

2016-04-24 12:23 567 查看

Nginx配置301跳转

1、找到/usr/local/nginx/conf/vhost/下面的.conf文件。
2、参考如下代码,见301跳转设置处:
server
{
listen 80;
#listen [::]:80;

server_name www.ju.com ju.com *.ju.com;

index index.php;
root  /data/wwwroot/ju_2_1;

#include other.conf;
#error_page   404   /404.html;
location / {
index index.php;
if (!-e  $request_filename)   {
rewrite ^/(.*)$ /index.php last;
}

#301 跳转设置
if ($host = 'ju.com') {
rewrite ^/(.*) http://www.ju.com/$1 permanent;
}

}

location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
}

location ~ .*\.(js|css)?$
{
expires      12h;
}

access_log  /home/wwwlogs/ju.log  access;
}

另外以上代码还开启了域名泛解析。

3、重启Nginx即可。

Apache配置301跳转

1、打开/etc/httpd/conf/http.conf文件,开启Apache重写功能:
#LoadModule rewrite_module modules/mod_rewrite.so
将这一行前的#号去掉。
2、在/etc/httpd/conf.d/目录下新建.conf文件,输入如下代码:
<VirtualHost *:80>
DocumentRoot "/home/wwwroot/ju"
ServerName www.ju.net
ServerAlias *.ju.net
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ju.net [NC]
RewriteRule ^(.*) http://www.ju.net$1 [R=301,L]
</IfModule>
</VirtualHost>
另外以上代码还开启了域名泛解析。
3、重启Apache。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: