我有这些网址变体
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]
我想使用 awk (sed 或任何其他工具)编辑它们来帮助编辑这些 URL 变体,以便它通过 apache 的 rewriterule 配置测试
请注意 (*)、(.)、(#)、(!) 等字符,甚至第 5 行上的空格如何编辑这些行集,以便一切看起来正确,可以部署到 apache 并通过 apache 配置测试httpd -t
?
编辑:v1
这是我正在寻找的能够通过 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]
注意:请注意第 5 行有空格,我必须转义该空格。因此,需要有一种方法来检测第 2 列中是否有空格,然后将其转义——沿着这条线。
答案1
我对 Apache 规则不太了解,所以这可能有点矫枉过正。但是,如果太多转义不是问题,您可以使用:
$ 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\]\
否则,要仅转义您提到的特定字符,请使用:
$ 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]
要执行相同但仅在第二个字段上的操作,请使用:
$ 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]
答案2
珀尔:
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]
在“^”或“http:”上分割行,转义所有非字母、数字、下划线、斜杠、连字符的字符;然后改造线路。