2015-04-07 112 views
0

我一堆舊網址的,像這樣的:重定向的所有文件中的子文件夾與htaccess的301

www.example.it/modules/prestapress/content.php?id=48 
www.example.it/modules/prestapress/content.php?id=47 
www.example.it/modules/prestapress/content.php?id=46 

我想改寫這個網址,以獲取ID和重定向到這些網址:

www.example.com/it/blog/48 
www.example.com/it/blog/47 
www.example.com/it/blog/46 

因此,網站將從.it更改爲.com,我需要一條規則將所有舊文章博客重定向到新文章網址(通過ID)。

這裏的.htaccess:

RewriteEngine On 
RewriteCond %{QUERY_STRING} \bid=(\d+) [NC] 
RewriteCond %{HTTP_HOST} ^example\.it [NC] 
RewriteRule ^modules/prestapress/content.php$ http://example.com/it/blog/%1? [R=301,L] 

,但以這種方式,如果我嘗試訪問:

example.it/modules/prestapress/content.php?id=48 

它重定向到

example.com/it/blog/ 

沒有mantaining的ID。

正確的問題! @ hjpotter92的建議是正確的,但打鬥VS這個其他規則,我有,這裏總結:

RewriteEngine On 

#For redirecting urls blog 

RewriteCond %{QUERY_STRING} \bid=(\d+) [NC] 
RewriteCond %{HTTP_HOST} ^example\.it [NC] 
RewriteRule ^modules/prestapress/content.php$ http://example.com/it/blog/%1-? [R=301,L] 

#FOR REDIRECT .IT to .COM for all others urls 

RewriteCond %{HTTP_HOST} ^(www\.)?example\.it$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 

所以問題是2點,我必須重定向到。它.COM,因爲.COM有相同的結構。它的,所以這個規則我解決了:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.it$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 

變化是文章的博客結構,併爲我們有這樣做的唯一的事情:

RewriteCond %{QUERY_STRING} \bid=(\d+) [NC] 
RewriteCond %{HTTP_HOST} ^example\.it [NC] 
RewriteRule ^modules/prestapress/content.php$ http://example.com/it/blog/%1-? [R=301,L] 

,但以這種方式網址重定向博客不起作用!

終於解開了,原來的規則是不正確的,TO完美的作品這裏的解決方案:

RewriteEngine On 

#For redirecting urls blog 

RewriteCond %{QUERY_STRING} \bid=(\d+) [NC] 
##WRONG RULE## RewriteCond %{HTTP_HOST} ^example\.it [NC] 
RewriteRule ^modules/prestapress/content.php$ http://example.com/it/blog/%1-? [R=301,L] 

#FOR REDIRECT .IT to .COM for all others urls 

RewriteCond %{HTTP_HOST} ^(www\.)?example\.it$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 
+0

你不想從'.it'移動到'.com'嗎? – hjpotter92

+0

是啊!對不起,我只是再次編輯,是一個錯誤。 –

+0

'blog'裏面沒有其他重寫規則嗎?它是否將您重定向到'example.com/blog /'或'example.com/it/blog/'? – hjpotter92

回答

-1

嘗試以下操作:

RewriteEngine On 
RewriteCond %{QUERY_STRING} \bid=(\d+) [NC] 
RewriteCond %{HTTP_HOST} ^www\.site\.it [NC] 
RewriteRule ^modules/prestapress/content.php$ http://www.new-site.com/blog/%1? [R=301,L] 

不要使用%1?從重定向的URL中移除的查詢字符串。

+0

不起作用!我也無法理解此RewriteCond%{QUERY_STRING} \ bid =(\ d +)[NC] –

+0

@ Wrs-evotechRacingParts'%{QUERY_STRING}'是匹配'?id ='部分網址。 – hjpotter92

+0

感謝%{QUERY_STRING} :)的解釋。是的mod_rewrite已啓用!.htaccess中沒有其他規則。重定向工作如果我的數字是:www.site.it/modules/prestapress但 –

相關問題