简体   繁体   中英

NGINX https://www.example.com/index.php to https://www.example.com/ redirect how?

I have this problem which I thought I had solved but I have not been able to solve it. I want to redirect:

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

I have tried to add:

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

But then it becomes an redirect error in the browser with an endless loop. Here is my config:

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";
   }
 }

I have googled and tested a couple of codes I found on the Internet but nothing helps. What am I missing?

Your best bet is to remove all links to index.php in your actual application and just serve any specific requests including index.php without trying to change such.

There should be very few to none once you deal with within the application since search engines will save the links without index.php .

Not sure where your current ...

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

... sits in the scheme of things (certainly not related to your index.php issue) and perhaps it should be deleted as likely to cause you issues unless you specifically want to redirect an index folder.

** EDIT **

Took another stab. Not tested but may be possible to break the loop. Try adding this just below error_page 500 502 503 504 /50x.html; in your original config

   # 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM