nginx 301简单重定向问题 rewrite ^/wp-login.php(.*) /amz.phpnginx last permanentt;

Nginx服务器中HTTP 301跳转到带www的域名的方法
投稿:goldensun
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Nginx服务器中HTTP 301跳转到带www的域名的方法,包括从HTTPS 301提示跳转等rewrite相关的方法,需要的朋友可以参考下
从nginx的官方文档 documentation, 正确的nginx https 301跳转到带www域名方法的方法如下:
HTTP 301跳转到带www域名方法
&&& 代码如下: server {
&&&&&&& listen&&&&&& 80;
&&&&&&& server_name& example.
&&&&&&& return&&&&&& 301 http://www.example.org$request_
&&& server {
&&&&&&& listen&&&&&& 80;
&&&&&&& server_name& www.example.
&&&&&&& ...
HTTPS 301跳转到带www域名方法
& 代码如下:&& server {
&&&&&&&&&&& listen 80;
&&&&&&&&&&& server_
&&&&&&&&&&& // $scheme will get the http protocol
&&&&&&&&&&& // and 301 is best practice for tablet, phone, desktop and seo
&&&&&&&&&&& return 301 $scheme://$request_
&&& server {
&&&&&&&&&&& listen 80;
&&&&&&&&&&& server_;
&&&&&&&&&&& // here goes the rest of your config file
&&&&&&&&&&& // example
&&&&&&&&&&& location / {
&&&&&&&&&&&&&&& rewrite ^/cp/login?$ /cp/login.
&&&&&&&&&&&&&&& // etc etc...
&&&&&&&&&&& }
要先用 nginx -v& 命令检查你所说使用的nginx的版本. 下面是对于旧版本的nginx301跳转到带www域名方法从 跳转到&
代码如下:server {
&&&&&&& server_name& ;
&&&&&&& rewrite ^(.*) $1
&&& server {
&&&&&&& server_name& ;
&&&&&&& #The rest of your configuration goes here#
所以需要两个server段。
从 跳转到&
代码如下:server {
&&&&&&& server_name& ;
&&&&&&& rewrite ^(.*) $1
&&& server {
&&&&&&& server_name& ;
&&&&&&& #The rest of your configuration goes here#
按上面设置后,用rewrite的方法跳转到指定的域名下,利于SEO
下面是我举例,从 跳转到 的部分nginx配置内容:
代码如下:server {
&&&&&&& server_name& ;
&&&&&&& rewrite ^(.*) $1
&&& server {
&&&&&&&&&& listen 80;
&&&&&&&&&& server_;
&&&&&&&&&& index index.php index.
&&&&&&&&&& ####
&&&&&&&&&& # now pull the site from one directory #
&&&&&&&&&& root /var/www//
&&&&&&&&&& # done #
&&&&&&&&&& location = /favicon.ico {
&&&&&&&&&&&&&&&&&&& log_not_
&&&&&&&&&&&&&&&&&&& access_
&&&&&&&&&& }
网上还有一种不用rewirte的 方法,如下:
代码如下:server {
&&&&&&& #listen 80 is default
&&&&&&& server_
&&&&&&& return 301 $scheme://$request_
&&& server {
&&&&&&& #listen 80 is default
&&&&&&& server_;
&&&&&&& ## here goes the rest of your conf...
因为return可以用于所有的版本,而rewrite可能因为版本的不同,导致301出错。而且可以直接停止执行匹配和搜索。
下面包含了http和https的。同一个服务器。
代码如下:server {
&&&&&&& listen 80;
&&&&&&& listen 443
&&&&&&& server_
&&&&&&& return 301 $scheme://$request_
&&& server {
&&&&&&& listen 80;
&&&&&&& listen 443
&&&&&&& server_;
&&&&&&& # rest goes here...
$scheme 变量只会包含http 如果你的服务器只监听80端口(默认是80端口)同时监听的选项中不含ssl关键字 . 不适用这个变量,就不能获得你所想的要的跳转结果。
将所有http强制跳到https, SSL (personal config on UNIX with IPv4, IPv6, SPDY, ...):
代码如下:#
# Redirect all www to non-www
&&& server_name&&&&&&&&& ;
&&& ssl_certificate&&&&& /
&&& ssl_certificate_key& /
&&& listen&&&&&&&&&&&&&& *:80;
&&& listen&&&&&&&&&&&&&& *:443
&&& listen&&&&&&&&&&&&&& [::]:80 ipv6only=
&&& listen&&&&&&&&&&&&&& [::]:443 ssl spdy ipv6only=
&&& return 301 $request_
# Redirect all non-encrypted to encrypted
&&& server_name&&&&&&&&& ;
&&& listen&&&&&&&&&&&&&& *:80;
&&& listen&&&&&&&&&&&&&& [::]:80;
&&& return 301 $request_
# There we go!
&&& server_name&&&&&&&&& ;
&&& ssl_certificate&&&&& /
&&& ssl_certificate_key& /
&&& listen&&&&&&&&&&&&&& *:443
&&& listen&&&&&&&&&&&&&& [::]:443
&&& # rest goes here...
# Redirect all www to non-www
&&& server_name&&&&&&&&& ;
&&& ssl_certificate&&&&& /
&&& ssl_certificate_key& /
&&& listen&&&&&&&&&&&&&& *:80;
&&& listen&&&&&&&&&&&&&& *:443
&&& listen&&&&&&&&&&&&&& [::]:80 ipv6only=
&&& listen&&&&&&&&&&&&&& [::]:443 ssl spdy ipv6only=
&&& return 301 $request_
# Redirect all non-encrypted to encrypted
&&& server_name&&&&&&&&& ;
&&& listen&&&&&&&&&&&&&& *:80;
&&& listen&&&&&&&&&&&&&& [::]:80;
&&& return 301 $request_
# There we go!
&&& server_name&&&&&&&&& ;
&&& ssl_certificate&&&&& /
&&& ssl_certificate_key& /
&&& listen&&&&&&&&&&&&&& *:443
&&& listen&&&&&&&&&&&&&& [::]:443
&&& # rest goes here...
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具nginx redirect loop, remove index.php from url [Nginx重定向循环,从URL移除index.php] - 问题-字节技术
nginx redirect loop, remove index.php from url
Nginx重定向循环,从URL移除index.php
问题 (Question)
I want any requests like /whatever/index.php, to do a 301 redirect to /whatever/.
I tried adding:
rewrite ^(.*/)index.php$ $1
location / {
The problem here, this rewrite gets run on the root url, which causes a infinite redirect loop.
I need a general solution
/ should serve the file webroot/index.php
/index.php, should 301 redirect to /
/a/index.php should 301 redirect to /a/
/a/ should serve the index.php script at webroot/a/index.php
Basically, I never want to show "index.php" in the address bar.
I have old backlinks that I need to redirect to the canonical url.
我想请像/whatever/index.php,做一个301重定向到/whatever/。我想加入:rewrite ^(.*/)index.php$ $1
location / {
问题就在这里,这个重写跑在根URL重定向,导致无限循环。编辑我需要一个通用的解决方案/要为文件webroot/index.php/index.php301,要重定向到//a/index.php应301重定向到/a//a/应为index.php脚本webroot/a/index.php基本上,我从来没有想表达“索引PHP在地址栏中。”。我已经老了,我需要的反向链接重定向到标准的网址。
最佳答案 (Best Answer)
Great question, with the solution , although it's much simpler here, and you know exactly what you need.
What you want here is to only perform the redirect when the user explicitly requests /index.php, but never redirect any of the internal requests that end up being served by the actual index.php script, as defined through the index directive.
This should do just that, avoiding the loops:
index index.
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
location / {
伟大的问题,与溶液虽然这是非常简单的,在这里,你知道你需要什么。你想要的是只执行重定向当用户明确要求/index.php,但不改变任何的内部要求,最终由实际服务index.php脚本,通过定义index指令。这就应该那样做,避免循环:server {
index index.
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
location / {
答案 (Answer) 2
location = /whatever/index.php {
return 301 $scheme:///whatever/;
Another benefit from doing it this way is that nginx does a return faster than a rewrite.
试用location = /whatever/index.php {
return 301 $scheme:///whatever/;
另一个受益于这样做是nginx重写更快的恢复比。
答案 (Answer) 3
Keep the first slash out of the match :
rewrite ^/(. +)/index.php$ $scheme://$1/
保持第一削减退出比赛:rewrite ^/(. +)/index.php$ $scheme://$1/
答案 (Answer) 4
location ~ /*/index.php {
rewrite ^/(.*)/(.*) http://www./$1
location /index.php {
return 301 /;
试试location ~ /*/index.php {
rewrite ^/(.*)/(.*) http://www./$1
location /index.php {
return 301 /;
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:you have been blocked

我要回帖

更多关于 urlrewrite 重定向 的文章

 

随机推荐