开发者

htaccess redirect from any subdomain to another domain and from directory to query URL

开发者 https://www.devze.com 2023-02-25 04:59 出处:网络
Im trying to use htaccess to redirect any subdomain and the root of one domain to another domain. example

Im trying to use htaccess to redirect any subdomain and the root of one domain to another domain. example

anysub.mysite.com or mysite.com redirects to www.anotheriste.com

and also mysite.com/stuffhere to www.anothersite.com/feed?v=stuffhere

So anything that is not a subdirectory gets redirected to the other domain and anything that is a subdirectory gets redirected to the URL with the query string. Can't seem to get it. One rewrite rules overwrites the other.

Edit: T开发者_Go百科his is what I got to work

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]


Try putting these rules in your .htacccess file:

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,L,QSA,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,L,QSA,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,L,QSA,NE]

R=301 will redirect with https status 301

L will make last rule

NE is for no escaping query string

QSA will append your existing query parameters

$1 is your REQUEST_URI

0

精彩评论

暂无评论...
验证码 换一张
取 消