use*_*656 14 rewrite nginx redirect
我正在尝试将所有访问 URL“ http://example.com/something ”的用户重定向到像“ http://answares.com/examples_com/something ”这样的 URL 。
我正在尝试使用这样的代码:
server {
listen 1.2.3.4:80;
server_name "example.com";
rewrite ^(.*) http://answares.com/examples_com$1 permanent;
}
Run Code Online (Sandbox Code Playgroud)
通过访问“ http://example.com/ ”我被重定向到“ http://answares.com/examples_com/ ”,但是通过访问“ http://example.com/something ”我被重定向到:“ http ://answares.com/something ”。
我尝试以不同的方式做到这一点,但我得到的最好的是:
http://answares.com/examples_com//something
Run Code Online (Sandbox Code Playgroud)
因为这两个斜线看起来很蹩脚。我在 Ubuntu 10.4 上使用 Nginx 0.7.65
Sha*_*den 17
如果您只想重定向/something,而没有其他 URL,则:
rewrite ^(/something.*) http://answares.com/examples_com$1 permanent;
Run Code Online (Sandbox Code Playgroud)
这将发送一个请求http://example.com/something/to http://answares.com/examples_com/something/,
并说,http://example.com/something/somewhere/page.html到http://answares.com/examples_com/something/somewhere/page.html
小智 7
您可以执行以下任一操作:
rewrite ^/(.*)$ http://answares.com/examples_com/$1 permanent;
Run Code Online (Sandbox Code Playgroud)
这给你带来:
$ curl -I http://xxxxx.com/some/example/url/here.html
HTTP/1.1 301 Moved Permanently
Server: nginx/0.8.53
Date: Mon, 05 Sep 2011 13:48:23 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://answares.com/examples_com/some/example/url/here.html
Run Code Online (Sandbox Code Playgroud)
或者
rewrite ^(/.*)$ http://answares.com/examples_com$1 permanent;
Run Code Online (Sandbox Code Playgroud)
你可以看到它也有效:
$ curl -I http://xxxxxx.com/some/example/url/here.html
HTTP/1.1 301 Moved Permanently
Server: nginx/0.8.53
Date: Mon, 05 Sep 2011 13:47:09 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://answares.com/examples_com/some/example/url/here.html
Run Code Online (Sandbox Code Playgroud)
nginx 重定向和 mod_rewrite 重定向的区别在于 nginx 在匹配之前不会删除服务器名称后面的斜杠(/)。
为了删除您拥有的双斜杠,您应该首先匹配正则表达式中没有括号的斜杠,然后应用匹配;或匹配所有内容并应用不带括号的匹配。使用一种或另一种方式只是品味问题;-)
| 归档时间: |
|
| 查看次数: |
108195 次 |
| 最近记录: |