htaccess从任何子域重定向到另一个域,并从目录重定向到查询URL

问题描述

|| 我试图使用htaccess将任何子域和一个域的根重定向到另一个域。 例 anysub.mysite.com或mysite.com重定向到www.anotheriste.com 以及mysite.com/stuffhere到www.anothersite.com/Feed?v=stuffhere 因此,不是子目录的所有内容都将重定向到另一个域,而子目录的任何内容都将使用查询字符串重定向到URL。似乎无法理解。一个重写规则将覆盖另一个规则。 编辑: 这就是我要工作的
Options +FollowSymlinks 
RewriteEngine on
RewriteRule ^$ http://www.site.com [R=301,L]

RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteCond  %{REQUEST_URI} ^/*\\/?
RewriteRule ^(.*)$ http://www.site.com/Feed?v=$1 [R=301,L]
    

解决方法

尝试将以下规则放入.htacccess文件中:
RewriteEngine on
Options +FollowSymlinks -MultiViews

# handles http redirect sub-dir
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^(.+\\.)?mysite\\.com$ [NC] 
RewriteRule ^/?(.*)$ http://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE]

# handles http redirect non sub-dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^(.+\\.)?mysite\\.com$ [NC] 
RewriteRule ^/?(.*)$ http://www.anotheriste.com/$1 [R=301,NE]

# handles https redirect sub-dir
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(.+\\.)?mysite\\.com$ [NC] 
RewriteRule ^/?(.*)$ https://www.anotheriste.com/feed?v=$1 [R=301,NE]

# handles https redirect non sub-dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(.+\\.)?mysite\\.com$ [NC] 
RewriteRule ^/?(.*)$ https://www.anotheriste.com/$1 [R=301,NE]
R = 301将以https状态301重定向 L将制定最后的规则 NE不用于转义查询字符串 QSA将附加您现有的查询参数 $ 1是您的REQUEST_URI