nginx /index.html to / rewrite -


i trying rewrite /index.html / seo purposes (stupid search engines confuse index.html / , penalize duplicate content) -- reconcile web analytics data.

i've tried every solution i've found on stackoverflow, nginx documentation, etc , have had no success. i'm thinking must have other configuration issue or else painfully obvious. first nginx installation -- used apache , iis!!

here default.conf:

server {     listen       80;     server_name  web.local;     #charset koi8-r;     #access_log  /var/log/nginx/log/host.access.log  main;      #error_page  404              /404.html;      # redirect server error pages static page /50x.html     #     error_page   500 502 503 504  /50x.html;     location = /50x.html {         root   /var/www/html;     }      # proxy php scripts apache listening on 127.0.0.1:80     #     #location ~ \.php$ {     #    proxy_pass   http://127.0.0.1;     #} 

here virtual.conf (commented out section recent attempt -- when uncommented gives 301 moved permanently error when attempt access www.domain.com/index.html):

server {     listen       80;     server_name  www.domain.com;      location / {         root   /var/www/html/domain.com;         index  index.html;         #if ($request_uri = /index.html) {         #    rewrite ^ http://www.domain.com permanent;         #}     } }  server {     listen 80;     server_name domain.com;     rewrite ^/(.*) http://www.domain.com/$1 permanent;     } 

http response headers cobaco's solution:

url: http://www.domain.com http/1.1 301 moved permanently server: nginx/1.2.8 date: thu, 16 may 2013 01:42:58 gmt content-type: text/html content-length: 184 connection: keep-alive location: http://domain.com/  redirecting url: http://domain.com/ http/1.1 301 moved permanently server: nginx/1.2.8 date: thu, 16 may 2013 01:42:58 gmt content-type: text/html content-length: 184 connection: keep-alive location: http://www.domain.com/ 

i figured line might causing problems: "location = /index.html {return 301 $scheme://domain.com/;}" added www. after "scheme://" -- let me know if bad thing do! resulted in following http response headers:

url: http://www.domain.com http/1.1 301 moved permanently server: nginx/1.2.8 date: thu, 16 may 2013 01:42:58 gmt content-type: text/html content-length: 184 connection: keep-alive location: http://www.domain.com/  redirecting url: http://www.domain.com/ http/1.1 301 moved permanently server: nginx/1.2.8 date: thu, 16 may 2013 01:42:58 gmt content-type: text/html content-length: 184 connection: keep-alive location: http://www.domain.com/ 

after more tinkering, following configuration want not ideal due if statement. suggestions?

server {   server_name  www.domain.com;   root /var/www/html/domain.com;   index index.html;   if ($request_uri = /index.html) {       return 301 http://www.domain.com/;   }   #location = /index.html {   #    return 301 $scheme://www.domain.com/;   #} }  server {   listen 80;   server_name domain.com;   return 301 $scheme://www.domain.com$request_uri; } 

you final solution totally fine.

if directive evil if inside location block. have return directive inside if block. don't see wrong that. reference: http://wiki.nginx.org/ifisevil

the infinite redirect loop in cobaco's solution because

  index  index.html; 

triggers round of location match. nginx trapped location = /index.html again after it's redirected http://www.domain.com/.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -