简体   繁体   中英

nginx rewrite rule for strip numbers from final

Can someone help me with this issue on nginx or using .htaccess?

I want to redirect an url like:

[http][www]domainName.tld/folderName/a-name-with-dashes-15-and-numbers-and-a-number-of-at-least-5-digits

Becomes

[http][www]domainName.tld/newFolderName/a-name-with-dashes-15-and-numbers

or

[http][www]domainName.tld/a-name-with-dashes-15-and-numbers

with www or without www.
a more real example (without domain):

/folderName/test-1-test-again-123456789

becomes

/newFolder/test-1-test-again
# or
/test-1-test-again

Thanks a lot

@Later Edit: Add Nginx Config from server block

    listen ip:80;

    server_name domain.tld www.domain.tld;

    root   /var/www/domain.tld/web;


    if ($http_host = "www.domain.tld") {
        rewrite ^ $scheme://domain.tld$request_uri? permanent;
    }


    index index.html index.htm index.php index.cgi index.pl index.xhtml;



    error_log /var/log/domain.tld/error.log;
    access_log /var/log/domain.tld/access.log combined;

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

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

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

    location /stats {

        index index.html index.php;
        auth_basic "Members Only";
        auth_basic_user_file /var/www/clients/client/web/web/stats/.htpasswd_stats;
    }

    location ^~ /awstats-icon {
        alias /usr/share/awstats/icon;
    }

    location ~ \.php$ {
        try_files /c91e3e9dc234ca8eec5e7e5309e2fcca.htm @php;
    }

    location @php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php5-fpm/web24.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }

    client_max_body_size 20M;

    location ~* ^.+\.(css|png|ico|ttf|rss|atom|js|jpg|jpeg|gif|zip|tgz|gz|rar|bz2|doc|xls|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;
    }

    location ~* ^/wp-admin/.*.(html|htm|shtml|php)$ {
       client_max_body_size 30M;
    }

    location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php)$ {
        types { }
        default_type text/plain;
    }

    location ~* ^/static/.*.(html|htm|shtml|php)$ {
        types { }
        default_type text/plain;
    }

    location / {
       try_files $uri $uri/ /index.php?$args;
    }

    location ~* (wp-comments-posts|wp-login)\.php$ {
       if ($http_referer !~ ^(http://www.domain.tld) ) {
          return 405;
       }
    }

Try something like this

location ~ '(.*)\-[0-9]{5,}$' {
  return 301 $scheme://$server_name$1;
}

This should strip all trailing numbers, if the numbers at the end of the url are more than 5 numbers

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