1

We are trying to remove the trailing slash from our URLs (primarily so that duplicate content is not being served).

I know there are many tutorials about how to do this, and I have succeeded in changing the core code so all links are to non-slash URLs. However I am struggling to do the rewrites for anyone who follows a link (or enters one manually) with the trailing slash.

All the tutorials I have read say to change the htaccess file, which after much digging I found out is not being used on our server. So I’m trying to use a rewrite in the DOMAIN.conf file (which is pulled in to the nginx.conf file).

I proposed this to our server hosts;

rewrite ^/(.*)/$ /$1 permanent;

But they said: “That appears to be correct, however we do not support this.” and gave me a link to an online htaccess tester.

Currently our .conf file contains this;

server {  listen <server IP>:80;  server_name www.DOMAIN.co.uk;  root <server root>;

location / {  index index.html index.php;  try_files $uri $uri/ @handler;  expires 30d; 
#auth_basic “Restricted”; 
#auth_basic_user_file /var/www/htpasswd;  }

location /app/ { deny all; }  location /includes/ { deny all; }  location /lib/ { deny all; }  location /media/downloadable/ { deny all; }  location /pkginfo/ { deny all; }  location /report/config.xml { deny all; }  location /var/ { deny all; }

location /. {  return 404;  }

location @handler {  rewrite / /index.php;  }

location ~ .php/ {  rewrite ^(.*.php)/ $1 last;  }

location ~ .php$ {  if (!-e $request_filename) { rewrite / /index.php last; }

expires off;  fastcgi_pass <password>;  fastcgi_param HTTPS $fastcgi_https;  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
# fastcgi_param MAGE_RUN_CODE default; 
# fastcgi_param MAGE_RUN_TYPE store;  include fastcgi_params;  } 


## 
# SEO REWRITES 
##  ...............

(edited to remove certain things and shortened to relevant info).

Can anyone help with what the rewrite would need to be?

2

Two things to note, duplicate content is not to be treated as an issue if you are using canonical links, as if you read the google and bing white docs they will follow that for what is indexed when dealing with duplicate content. Just asking, is the juice worth the squeeze? But this is what I have as my store config and don't see to have the trailing / issue as you describe.

store

server {
## Limits remote clients to no more than 10 concurrently "open"
## connections per remote ip address.
#limit_conn arbeit 10;
listen      80;
server_name store.domain.xxx;
root /usr/share/nginx/html/store;
access_log /usr/share/nginx/html/logs/access.log;
error_log /usr/share/nginx/html/logs/error.log notice;
dav_methods  PUT; #for rest api
location / {
    index index.html index.php; ## Allow a static html file to be shown first
    try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
    expires 30d; ## Assume all files are cachable
}
## These locations would be hidden by .htaccess normally
location ^~ /app/                { deny all; }
location ^~ /includes/           { deny all; }
location ^~ /lib/                { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/            { deny all; }
location ^~ /report/config.xml   { deny all; }
location ^~ /var/                { deny all; }
location ^~ /var/cache/packages/ { allow all; }


location /var/export/ { ## Allow admins only to view export folder
    auth_basic           "Restricted"; ## Message shown in login window
    auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
    autoindex            on;
}

location /api {
    # rewrite ^/api/rest /api.php?type=rest  break;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://localhost:8888;
}

location  /. { ## Disable .htaccess and other hidden files
    return 404;
}

location @handler { ## Magento uses a common front handler
    rewrite / /index.php;
}

location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
    rewrite ^(.*.php)/ $1 last;
}

## Execute PHP scripts
location ~ .php$ { ## Execute PHP scripts
    expires off; ## Do not cache dynamic content
    include /etc/nginx/fastcgi_params;



    fastcgi_pass   unix:/tmp/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param MAGE_RUN_CODE default;
    fastcgi_param MAGE_RUN_TYPE store;
}
}
  • 1
    I am not an "expert" in SEO, I've just been told what to do (by my boss who has probably been told by our SEO consultants, both know more than me!). However I will have a look in to those whitepapers and ask the question.We still don't have this resolved and we have moved server hosts since. I'm at a bit of a loss... – James Oct 29 '13 at 16:22
  • 1
    well it's like dead skin cells but yes removing them does make people feel better. but rereading your question, rewrite ^/(.*)/$ /$1 permanent; I think is the way to go, and if the host says that you should check .htaccess rules for nginx, maybe they don't really know what they are doing? Cause I may be off here but I don't think you can turn off the regex on the nginx phrasing of the config file so (.*) ie: capture group, one line, any character should be just fine. Just saying it seems like the host is blow smoke – jeremy.bass Oct 29 '13 at 16:36
  • I've accepted this as the answer because :as I alluded to in my comments I believe it was just a matter of time and Google being... "elusive" let's say... Also @jeremy.bass made a well reasoned attempt despite all the uncertainty. We now have much better rankings. I'm not sure if we used canonical URLs properly back then but I have no doubt they are the correct way to go. – James May 24 '18 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.