use*_*308 13 nginx url-rewriting
使用nginx的,我想重定向的所有子域example.com
来www.example.com
.
我在这里看到重定向将非www重定向到www或反之亦然,但我也希望www2.site.com blabla.site.com
被重定向.我有域的通配符dns.
对于apache,可以通过以下方式轻松完成:
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)
以下似乎工作,但不建议根据ifisevil页面.
if ($http_host !~ "www.site.com"){
rewrite ^(.*)$ http://www.example.com$request_uri redirect;
}
Run Code Online (Sandbox Code Playgroud)
cob*_*aco 21
在nginx中执行此操作的最佳方法是使用两个服务器块的组合:
server {
server_name *.example.org;
return 301 $scheme://example.org$request_uri;
}
server {
server_name www.example.org;
#add in further directives to serve your content
}
Run Code Online (Sandbox Code Playgroud)
我已经在笔记本电脑上测试了这个,因为你报告它不起作用.我在本地得到以下结果(在添加www2.test.localhost
和www.test.localhost
我的/etc/hosts
,以及nginx配置位,并重新加载nginx之后):
$ curl --head www2.test.localhost
HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.6
Date: Thu, 07 Mar 2013 12:29:32 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.test.localhost/
Run Code Online (Sandbox Code Playgroud)
所以是的,这绝对有效.
VBa*_*art 13
server {
server_name .example.com;
return 301 http://www.example.com$request_uri;
}
server {
server_name www.example.com;
[...]
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
归档时间: |
|
查看次数: |
28695 次 |
最近记录: |