Text-Processing
重新格式化 Apache RewriteRule
我有這些網址變體
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 列中是否有空間,然後將其轉義——沿著這條線。
我不太了解 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]