使用htaccess 301重定向子文件夹中的所有文件

时间:2015-04-07 07:27:47

标签: .htaccess redirect

我有一堆旧网址,如下:

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/

没有保留身份证。

纠正问题! @ hjpotter92的提议是正确的,但是FIGHTS 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个站点,我必须将.it重定向到.com,因为.com具有相同的.it结构,所以通过这个规则我解决了:

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]

但是通过这种方式,网址博客的重定向无效!

最终解决了,原始规则不正确,在这里完美地运作解决方案:

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]

1 个答案:

答案 0 :(得分:-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?从重定向的网址中删除查询字符串。