Reformatando o Apache RewriteRule

1

Eu tenho essa variante de URLs

RewriteRule ^/trendy/the-reason-for-example/? http://www.example.com/trendy/the-reason [NC, L, R=301]
RewriteRule ^/lol/2015/10..._for-example http://www.example.com/lol/the-reason [NC, L, R=301]
RewriteRule ^/sports/this-one***-as-well/ http://www.example.com/sports/this-one [NC, L, R=301]
RewriteRule ^/buzz/the-#reason-for-buzz http://www.example.com/buzz/buzz-sells [NC, L, R=301]
RewriteRule ^/omg/ what-the-hell http://www.example.com/omg/wthell [NC, L, R=301]
RewriteRule ^/hash/HELL-YEAH http://www.example.com/hash/oh-yes [NC, L, R=301]
RewriteRule ^/celeb/he-did-it! http://www.example.com/celeb/we-believe [NC, L, R=301]

e eu quero editá-los usando o awk (sed ou qualquer outra ferramenta) para ajudar a editar essa variante de URLs para passar no rewriterule config test do apache

Observe os caracteres como (*), (.), (#), (!) e até o espaço na 5ª linha Como faço para editar esse conjunto de linhas para que tudo pareça correto para ser implantado no apache e passar no teste de configuração do apache httpd -t ?

EDIT: v1

Aqui está algo que estou procurando e que passará no teste do apache

RewriteRule ^/trendy/the-reason-for-example/? http://www.example.com/trendy/the-reason [NC, L, R=301]
RewriteRule ^/lol/2015/10\.\.\._for-example http://www.example.com/lol/the-reason [NC, L, R=301]
RewriteRule ^/sports/this-one\*\*\*-as-well/ http://www.example.com/sports/this-one [NC, L, R=301]
RewriteRule ^/buzz/the-\#reason-for-buzz http://www.example.com/buzz/buzz-sells [NC, L, R=301]
RewriteRule ^/omg/\ what-the-hell http://www.example.com/omg/wthell [NC, L, R=301]
RewriteRule ^/hash/HELL-YEAH http://www.example.com/hash/oh-yes [NC, L, R=301]
RewriteRule ^/celeb/he-did-it\! http://www.example.com/celeb/we-believe [NC, L, R=301]

NOTA: por favor note que a linha 5 tinha espaço e eu tive que escapar do espaço. Portanto, é necessário que haja uma maneira de detectar se há espaço na coluna 2 e, em seguida, escape - algo ao longo dessa linha.

    
por uberrebu 14.10.2015 / 17:51

2 respostas

0

Eu realmente não sei muito sobre as regras do Apache, então isso é provavelmente um exagero. No entanto, se escapar demais não for um problema, você pode usar:

$ perl -ne 'print quotemeta()' file
RewriteRule\ \^\/trendy\/the\-reason\-for\-example\ http\:\/\/www\.example\.com\/trendy\/the\-reason\ \[NC\,\ L\,\ R\=301\]\
RewriteRule\ \^\/lol\/2015\/10\.\.\._for\-example\ http\:\/\/www\.example\.com\/lol\/the\-reason\ \[NC\,\ L\,\ R\=301\]\
RewriteRule\ \^\/sports\/this\-one\*\*\*\-as\-well\ http\:\/\/www\.example\.com\/sports\/this\-one\ \[NC\,\ L\,\ R\=301\]\
RewriteRule\ \^\/buzz\/the\-\#reason\-for\-buzz\ http\:\/\/www\.example\.com\/buzz\/buzz\-sells\ \[NC\,\ L\,\ R\=301\]\
RewriteRule\ \^\/omg\/\ what\-the\-hell\ http\:\/\/www\.example\.com\/omg\/wthell\ \[NC\,\ L\,\ R\=301\]\
RewriteRule\ \^\/hash\/HELL\-YEAH\ http\:\/\/www\.example\.com\/hash\/oh\-yes\ \[NC\,\ L\,\ R\=301\]\
RewriteRule\ \^\/celeb\/he\-did\-it\!\ http\:\/\/www\.example\.com\/celeb\/we\-believe\ \[NC\,\ L\,\ R\=301\]\

Caso contrário, para escapar apenas dos caracteres específicos que você mencionou, use:

$ sed 's#[*.#!]#\&#g' file
RewriteRule\ ^/trendy/the-reason-for-example\ http://www\.example\.com/trendy/the-reason\ [NC,\ L,\ R=301]
RewriteRule\ ^/lol/2015/10\.\.\._for-example\ http://www\.example\.com/lol/the-reason\ [NC,\ L,\ R=301]
RewriteRule\ ^/sports/this-one\*\*\*-as-well\ http://www\.example\.com/sports/this-one\ [NC,\ L,\ R=301]
RewriteRule\ ^/buzz/the-\#reason-for-buzz\ http://www\.example\.com/buzz/buzz-sells\ [NC,\ L,\ R=301]
RewriteRule\ ^/omg/\ what-the-hell\ http://www\.example\.com/omg/wthell\ [NC,\ L,\ R=301]
RewriteRule\ ^/hash/HELL-YEAH\ http://www\.example\.com/hash/oh-yes\ [NC,\ L,\ R=301]
RewriteRule\ ^/celeb/he-did-it\!\ http://www\.example\.com/celeb/we-believe\ [NC,\ L,\ R=301]

Para fazer o mesmo, mas apenas no segundo campo, use:

$ perl -lne '/(.+\^)(.*)( http.*)/; ($k,$l,$m)=($1,$2,$3); $l=~s/[ *.#!]/\$&/g; print "$k$l$m";' file
RewriteRule ^/trendy/the-reason-for-example http://www.example.com/trendy/the-reason [NC, L, R=301]
RewriteRule ^/lol/2015/10\.\.\._for-example http://www.example.com/lol/the-reason [NC, L, R=301]
RewriteRule ^/sports/this-one\*\*\*-as-well http://www.example.com/sports/this-one [NC, L, R=301]
RewriteRule ^/buzz/the-\#reason-for-buzz http://www.example.com/buzz/buzz-sells [NC, L, R=301]
RewriteRule ^/omg/\ what-the-hell http://www.example.com/omg/wthell [NC, L, R=301]
RewriteRule ^/hash/HELL-YEAH http://www.example.com/hash/oh-yes [NC, L, R=301]
RewriteRule ^/celeb/he-did-it\! http://www.example.com/celeb/we-believe [NC, L, R=301]
    
por 14.10.2015 / 18:30
0

Perl:

perl -pe '
  @f = split /\^|\s+http:/;
  $f[1] =~ s{([^\w/-])}{\\$1}g;
  $_ = $f[0] . "^" . $f[1] . " http:" . $f[2]
' file
RewriteRule ^/trendy/the-reason-for-example http://www.example.com/trendy/the-reason [NC, L, R=301]
RewriteRule ^/lol/2015/10\.\.\._for-example http://www.example.com/lol/the-reason [NC, L, R=301]
RewriteRule ^/sports/this-one\*\*\*-as-well http://www.example.com/sports/this-one [NC, L, R=301]
RewriteRule ^/buzz/the-\#reason-for-buzz http://www.example.com/buzz/buzz-sells [NC, L, R=301]
RewriteRule ^/omg/\ what-the-hell http://www.example.com/omg/wthell [NC, L, R=301]
RewriteRule ^/hash/HELL-YEAH http://www.example.com/hash/oh-yes [NC, L, R=301]
RewriteRule ^/celeb/he-did-it\! http://www.example.com/celeb/we-believe [NC, L, R=301]

Divida a linha em "^" ou "http:", esacape todos os caracteres que não sejam alfa, numéricos, sublinhados, barra, hífen; então reformule a linha.

    
por 14.10.2015 / 19:04