2014-09-22 50 views
0

我有这个问题,我想我已经解决了,但我一直无法解决它。我想重定向:NGINX https://www.example.com/index.php到https://www.example.com/重定向如何?

---------------------------------------------------------------------------- 
|     FROM     |    TO    | 
---------------------------------------------------------------------------- 
|  https://www.wknet.se/index.php  |  https://www.wknet.se/  | 
---------------------------------------------------------------------------- 

我已尝试添加:

location = /index.php { 
     return 301 https://www.wknet.se; 
} 

但随后变成与无限循环浏览器中的重定向错误。这是我的配置:

server { 
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on; 
    server_name wknet.se www.wknet.se; 

    add_header Strict-Transport-Security max-age=15768000; 
    return 301 https://www.wknet.se$request_uri; 
} 

server { 
    listen 443 ssl; 
    server_name wknet.se; 

    ssl_certificate /etc/nginx/ssl/SSL.crt; 
    ssl_certificate_key /etc/nginx/ssl/KEY.key; 

    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; 
    ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM; 
    ssl_prefer_server_ciphers on; 

    return 301 https://www.wknet.se$request_uri; 
} 

server { 
    listen 443 ssl; 
    server_name www.wknet.se; 

    root /var/www/wknet.se/html; 
    index index.php index.html index.htm; 

    ssl_certificate /etc/nginx/ssl/SSL.crt; 
    ssl_certificate_key /etc/nginx/ssl/KEY.key; 

    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; 
    ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM; 
    ssl_prefer_server_ciphers on; 

    error_page 404 /404.html; 
    error_page 500 502 503 504 /50x.html; 

    location/{ 
     try_files $uri $uri/ =404; 
    } 

    location = /50x.html { 
     root /usr/share/nginx/html; 
    } 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 
    } 

    location = /index { 
     return 301 https://www.wknet.se; 
    } 

    location ~ /\.ht { 
     deny all; 
    } 

    location = /favicon.ico { 
     log_not_found off; 
     access_log off; 
    } 

    location = /robots.txt { 
     allow all; 
     log_not_found off; 
     access_log off; 
    } 

    location ~ /\. { 
     deny all; 
     error_log off; 
     log_not_found off; 
    } 

    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ { 
     log_not_found off; 
     expires 365d; 
    } 

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|png|gif|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { 
     access_log off; 
     log_not_found off; 
     expires max; 
     add_header Pragma public; 
     add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
    } 

    location ~* \.(7z|ai|class|css|csv|ejs|eps|flv|html?|jar|jpe?g|js|json|lzh|m4a|m4v|mov|mp3|pdf|pict|pls|ps|psd|swf|tiff?|txt|webp)$ { 
     access_log off; 
     log_not_found off; 
     expires max; 
     add_header Pragma public; 
     add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
    } 
} 

我google和测试了一些代码,我在互联网上找到,但没有什么帮助。我错过了什么?

回答

0

最好的办法是在您的实际应用中删除所有到index.php的链接,并且只是为任何特定请求提供服务,包括index.php而不尝试更改此类。

一旦你在应用程序内处理,应该很少到没有,因为搜索引擎将保存链接,而没有index.php

不知道在您的当前...

location = /index { 
     return 301 https://www.wknet.se; 
    } 

...坐落在东西(当然不是与你的index.php问题)的方案,也许它应该被删除可能导致你除非你特别想重定向index文件夹。

**编辑**

接过另一个刺。未经测试,但有可能打破循环。 尝试在原有的配置添加此仅低于error_page 500 502 503 504 /50x.html;

# We need to check if the user has specifically requested "index.php" 
    # and only redirect if they have ($request_uri variable). 
    # Internal redirects ($uri variable) should be left alone to avoid a redirect loop. 
    if ($request_uri ~ ^(/index\.php)$) { 
     return 301 https://www.wknet.se; 
    } 
+0

它不工作,我添加了建议,但我不断收到重定向无限循环错误。看看 - > [https://www.wknet.se/](https://www.wknet.se/)。我无法弄清楚。我如何做一个嵌套? – damircalusic 2014-09-22 18:46:20

+0

对不起......这将始终导致重定向循环。我会修改答案。 – Dayo 2014-09-22 19:14:25

+0

关于如何摆脱无限循环,是否还有其他解决方案? :( – damircalusic 2014-09-22 19:22:29