简体   繁体   中英

Perl rewrite old URLS to seo friendly

I am trying to fix my old URLS which have bad characters in them for all .html request

I want to replace the following instance with a dash ( - ):

  • Space
  • Comas
  • Plus signs
  • Parentheses
  • Apostrophes
  • Double dashes with a single dash
  • Then all Upper and lower cases

The following has been implemented and is not working on the sever

I've re-written the perl fragment nginx is using as follows:

perl_set $old_uri 'sub {
my $r = shift;
my $uri = $r->uri;

$uri = lc $uri;
$uri =~ s/[+, ()\']/-/g;
$uri =~ s/--+/-/g;

return $uri;
}';

This is switching everything to lowercase, converting the requested symbols to dashes, then compacting multiple dashes in a row into one

The perl rules for this are

    perl_set $old_uri 'sub {
    my $r = shift;
    my $uri = $r->uri;

    $uri = lc $uri; # Upper to lower
    $uri =~ s/[^a-z0-9-.\/]/-/g;
    $uri =~ s/--+/-/g; # turn double -- to -

    return $uri;
   }';

The location block which calls this is

  location ~ [^a-z0-9-.\/].*.html {
        rewrite ^ $old_uri permanent;
    }

Let me know if I need to provide more info

您可以将任何特殊字符替换为破折号/连字符

$url=~ s/\W+/-/g;

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