Apache RewriteRuleの再フォーマット

Apache RewriteRuleの再フォーマット

次のURLバリアントがあります。

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の書き換え設定テストに合格したいと思います。

(*)、(。)、(#)、(!)の文字と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]

同じことを行いますが、2番目のフィールドでのみ実行するには、次のようにします。

$ 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]

おすすめ記事