htaccess重定向域,但不是单个页面?

时间:2013-06-27 20:13:47

标签: apache .htaccess redirect

我的网站已更改域名。我有4个特定页面需要重定向到新域上的新等效页面。除此之外,我希望整个域重定向到新域。

这是我的代码:

Options +FollowSymLinks
RewriteEngine on

RewriteBase /

# Redirect individual pages.
RewriteRule http://olddomain.net/subdirectory/subpage1/$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]

RewriteRule http://olddomain.net/subdirectory/subpage2/$ https://www.newdomain.com/newsub/newpage2/ [R=301,L]

RewriteRule http://olddomain.net/subdirectory/subpage3/$ https://www.newdomain.com/newsub/newpage3/ [R=301,L]

RewriteRule http://olddomain.net/subdirectory/subpage4/$ https://www.newdomain.com/newsub/newpage4/ [R=301,L]

RewriteRule (.*) http://www.newdomain.com/ [R=301,L] 

我也试过写这样的RewriteRules,但它有同样的效果:

RewriteRule ^/subdirectory/subpage1/$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]

这似乎只执行最后一行,也就是说,无论输入什么olddomain.net网址,它总是重定向到http://www.newdomain.com/。如何获取要重定向的各个网址?

解决:这是我最终使用的:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/subdirectory/subpage1/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage1/ [R=301,L]

RewriteCond %{REQUEST_URI} ^/subdirectory/subpage2/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage2/ [R=301,L]

RewriteCond %{REQUEST_URI} ^/subdirectory/subpage3/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage3/ [R=301,L]

RewriteCond %{REQUEST_URI} ^/subdirectory/subpage4/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage4/ [R=301,L]

Redirect 301 / http://www.newdomain.com/

1 个答案:

答案 0 :(得分:0)

RewriteRule在Path not not Uri上运行,你试过的第二种方法工作正常,只需删除前导斜杠:

RewriteRule ^subdirectory/subpage1/?$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]

RewriteRule ^subdirectory/subpage2/?$ https://www.newdomain.com/newsub/newpage2/ [R=301,L]

RewriteRule ^subdirectory/subpage3/?$ https://www.newdomain.com/newsub/newpage3/ [R=301,L]

RewriteRule ^subdirectory/subpage4/?$ https://www.newdomain.com/newsub/newpage4/ [R=301,L]

RewriteRule (.*) http://www.newdomain.com/ [R=301,L]