2011-12-07 15 views
2

我使用如何自動化的.htaccess的定義重定向基於內容

grep -HEri "Title\:(content)" ./www.livesite.com/ > Livesite.txt 

grep -HEri "Title\:(content)" ./www.devsite.com/ > Devsite.txt 

找到對具有匹配或相似的內容,我可以用正則表達式指定的路徑。當^/example_found_live_path.html中的內容與^/different/found_devsite_path中的內容匹配時,我想要將一行添加到開發站點的.htaccess文件,創建301從活動站點路徑到dev站點上找到的路徑的重定向,如下所示:

redirect 301 ^/example_live_path.html ^/different/devsite_path 

期望的結果是,在推出後,所有搜索引擎條目和鏈接從當前直播網站的網頁重定向到的頁面與在開發站點匹配的標題。 我覺得這是sed,grep和xargs的工作,但不知道如何構建命令。 是這樣的:

grep -HEri "Title\:(content)" ./www.livesite.com/ | xargs 'echo %1; grep -HEri %2 ./www.devsite.com' | xargs 'sed "$a\nredirect 301 \^%1 \^%2\n' .htaccess 

在此先感謝!

+0

你能提供樣品的輸入和期望的輸出嗎? –

+0

輸入內容爲:實時站點上的所有文件以及唯一的內容標識符。這個想法是識別一個內容,比如頁面特有的「標題:($ the_page_title)」。然後,腳本會將舊站點中的內容與新開發站點中的內容匹配,以.htaccess格式返回遷移所需的URL重定向。輸出是一個在301重定向語法中每行具有1個apache指令的文件。 –

+0

當我說「sample」時,我的意思是「剪切一個文件並放在這裏,就像它」,然後輸入你期望得到的結果。從你的問題我甚至無法理解你正在試圖做什麼,還有的裏grep和輸出之間沒有聯繫,到目前爲止(因爲你不顯示輸入的是什麼樣子,所以我不知道該怎樣提取) 。 –

回答

0

你已經運行了兩個文件列表:

grep -HEri "Title\:(content)" ./www.livesite.com/ > Livesite.txt 
grep -HEri "Title\:(content)" ./www.devsite.com/ > Devsite.txt 

,並得到:(有每一個找到的文件也許多個匹配數)

(Livesite.txt) 
./www.livesite.com/foo/bar1.xhtml:...Title:(content1)........1a 
./www.livesite.com/foo/bar1.xhtml:......Title:(content3)........1b 
./www.livesite.com/goo/car.xhtml:....Title:(content4)........1c 
... 

(Devsite.txt) 
./www.devsite.com/bar_dev.html:......Title:(content2)...2a 
./www.devsite.com/far_zoo.html:...Title:(content4).........2b 
./www.devsite.com/far_zoo.html:...Title:(content3).........2c 
... 

連接結果應該是:

1a - null 
1b - 2c 
1c - 2b 
null - 2a 

這裏是腳本內加入了兩個列表的匹配內容:

while IFS=: read livepath line1; do 
    matching1="${line1#*Title:(}" 
    matching1="${matching1%)*}" 

    matched=0 
    matched_devpath= 
    while IFS=: read devpath line2; do 
     matching2="${line2#*Title:(}" 
     matching2="${matching2%)*}" 
     if [ "$matching1" = "$matching2" ]; then 
      matched=1 
      matched_devpath="$devpath" 
      break 
     fi 
    done <Devsite.txt 

    if [ "$matched" = 1 ]; then 
     url1="${livepath#./www.livesite.com}" 
     url2="${devpath#./www.devsite.com}" 
     echo "301 ^$url1 ^$url2" 
    fi 
done <Livesite.txt 

,其結果應該是:

redirect 301 ^/foo/bar1.html ^/far_zoo.html 
redirect 301 ^/goo/car.html ^/far_zoo.html 

嗯..祝你好運!

相關問題