Regular-Expression

用於 apache 重寫的模式正則表達式

  • June 23, 2016

我有一堆 URL,數以千計,我需要重定向,並且 URL 中有 thre 主要模式

第一個模式 有一個短劃線-開始標題網址並想要刪除該短劃線例如

www.example.com/buzz/news/the-written--malice-of-rdicile www.example.com/buzz/news/the-written-malice-of-rdicile

www.example.com/break/news/5-kids--were-found-rigmaroling-armed  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

第二種模式

網址之間有兩個破折號--需要更改為一個破折號,- 例如

www.example.com/buzz/news/-the-written-malice-of-rdicile www.example.com/buzz/news/the-written-malice-of-rdicile

www.example.com/break/news/-5-kids-were-found-rigmaroling-armed  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

第三種模式 有一個破折號-結束標題網址並想要刪除該破折號例如

www.example.com/buzz/news/the-written-malice-of-rdicile- www.example.com/buzz/news/the-written-malice-of-rdicile

www.example.com/break/news/5-kids-were-found-rigmaroling-armed-  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

第四種模式 在網址中有起始破折號、雙破折號和結束破折號的組合,並且想要刪除起始破折號和結束破折號並將雙破折號更改為單破折號例如..g

www.example.com/buzz/news/-the-written--malice-of-rdicile- www.example.com/buzz/news/the-written-malice-of-rdicile

www.example.com/break/news/-5-kids-were-found--rigmaroling-armed-  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

www.example.com/break/news/-5-kids-were-found-rigmaroling-armed-  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

這些模式大約有 5000 行,因此尋找一種使用正則表達式或其他方法的方法,以便能夠執行所有 5000 行並將 apache 重寫應用於 URL

非常感謝您的幫助。先感謝您

PS會繼續編輯讓問題更清晰;任何問題?請問我。

總而言之,看起來這個 LEFT 將是要重定向的 URL,而 RIGHT 將是使用者需要訪問的最終 URL

www.example.com/buzz/news/the-written--malice-of-rdicile www.example.com/buzz/news/the-written-malice-of-rdicile 

www.example.com/break/news/5-kids--were-found-rigmaroling-armed  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

www.example.com/buzz/news/-the-written-malice-of-rdicile www.example.com/buzz/news/the-written-malice-of-rdicile

www.example.com/break/news/-5-kids-were-found-rigmaroling-armed  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

www.example.com/buzz/news/-the-written--malice-of-rdicile- www.example.com/buzz/news/the-written-malice-of-rdicile

www.example.com/break/news/-5-kids-were-found--rigmaroling-armed-  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

www.example.com/break/news/-5-kids-were-found-rigmaroling-armed-  www.example.com/break/news/5-kids-were-found-rigmaroling-armed

最終我得到了匹配的源 URL 和目標 URL,並且不需要使用正則表達式來簡化或進行任何優化。

所以只是逐行進行,所以 5000ish 行重定向..是的,我知道

引用自:https://unix.stackexchange.com/questions/265358