2015-10-06 119 views
1

我看到很多類似的問題,但沒有幫助我至今:防止進入搜索引擎不友好的URL,然後重定向到搜索引擎友好的(規範)的網址

我有我的下/song/song.php腳本的邏輯?url = lang/params(seo-不友好的URL)。我用這個改寫有一個友好的URL並加載從搜索引擎不友好的一個內容,使用參數語言來區分:

RewriteRule ^songs-and-chords/(.*?)(/*)?$ /song/song.php?url=en/$1 [NC,L] 
RewriteRule ^canzoni-e-accordi/(.*?)(/*)?$ /song/song.php?url=it/$1 [NC,L] 

現在我想阻止訪問/song/song.php,迫使一個重定向到搜索引擎友好的一個,所以這樣的事情,使用%{} THE_REQUEST使外部重定向:

RewriteCond %{THE_REQUEST} .*?song/song\.php\?url=it/(\S*) [NC] 
RewriteRule /canzoni-e-accordi/$1 [R,L] 
RewriteCond %{THE_REQUEST} .*?song/song\.php\?url=en/(\S*) [NC] 
RewriteRule /songs-and-chords/$1 [R,L] 
RewriteCond %{THE_REQUEST} ^.*?song/song\.php.*$ [NC] 
RewriteRule /songs-and-chords/ [R,L] 

但是這個規則執行...什麼我做錯了不?我如何以良好的方式調試這個?

(我已經在song.php腳本中使用rel =「canonical」,但仍然希望腳本根本無法訪問)。

回答

1

你在做什麼錯了,你的RewriteRule語句的模式丟失。此外,重寫的URL將使用%1參考,而不是$1

RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\?url=it/(\S+) [NC] 
RewriteRule^/canzoni-e-accordi/%1? [R=301,L] 
RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\?url=en/(\S+) [NC] 
RewriteRule^/songs-and-chords/%1? [R=301,L] 
RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\s [NC] 
RewriteRule^/songs-and-chords/ [R=301,L] 
+0

謝謝,這是正確的。唯一的問題是:這個URL /song/song.php?url=it/pink-floyd/the-wall 重定向到 /canzoni-e-accordi/pink-floyd/the-wall?url = it/pink -floyd/the-wall 如何擺脫附加的url參數? QSA標誌未設置,因此它來自何處? (GET之後的「\」是什麼)? – Fabbio

+1

@Fabbio將'%1'更改爲'%1?'將修復重定向的url中添加的查詢字符串。至於'GET \'字符串;發送給服務器的請求格式爲:'GET /song/song.php?url=it/pink-floyd/the-wall HTTP/1.1'。 'GET'後我逃離了這個空間。你也可以使用'\ s'而不是'\' – hjpotter92

+0

就像一個魅力!但我現在有點失落......%1的含義是什麼?這不是正則表達式的東西,對吧?我也不清楚爲什麼我應該用「^」來啓動一個RewriteRule。你能建議關於這些問題的任何好的文檔頁面嗎? – Fabbio