.htaccess将olddomain /文件夹重定向到newdomain / otherfolder

时间:2015-11-16 15:12:14

标签: apache .htaccess mod-rewrite redirect

我在httpd.config中使用它,它可以工作:

<VirtualHost *:80>
ServerName olddomain.com
ServerAlias www.olddomain.com
Redirect permanent /FolderName/Filename_with_underscores.html http://newdomain.com/some-folder-with-dashes/?lang=fr
Redirect permanent /FolderName/Other_filename.html http://newdomain.com/some-other-folder/?lang=fr
Redirect permanent / http://newdomain.com/
</Virtualhost>

现在我想把它放在网站根目录下的.htaccess文件中。 我尝试了几件事,但它一直都失败了。 有谁知道如何将这个httpd重定向翻译成.htaccess?

网址中有大写字母,短划线和下划线......

以下代码有效(但它只是故事的一半:它将所有(www。)olddomain.com重定向到newdomain.com上的所需文件夹):

RewriteCond %{HTTP_HOST} ^olddomain\.com\$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http://newdomain.com/some-folder-with-dashes/?lang=fr" [R=301,L]

日Thnx

1 个答案:

答案 0 :(得分:1)

您可以在根.htaccess中使用

RewriteEngine on

# All rules only for olddomain.com
RewriteCond %{HTTP_HOST} !^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ - [L]

RewriteRule ^FolderName/Filename_with_underscores\.html$ http://newdomain.com/some-folder-with-dashes/?lang=fr [NC,R=301,L]
RewriteRule ^FolderName/Other_filename\.html$ http://newdomain.com/some-other-folder/?lang=fr [NC,R=301,L]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]