简体   繁体   中英

htaccess redirect entire domain/folder to another domain

I am not a friend of htaccess and have bumped into a problem I can't seem to google a solution for. So here is hoping that you guys can help.

I have a multisite instalation where I am trying to redirect all trafic to mydomain.com/anothersite (and all sub folders and query strings) to anothersite.com and at the same time have all requests to anothersite.com rewrite to requesting the server to look up the anothersite folder.

Basically the server folder setup is as follows: in the root we have a website in the root we have a folder "anothersite" that contains another site

So all requests to mydomain.com should show the root folder while all requests to anothersite.com should get rewritten.

Hope that made sence

Here is what I have (it almost works but not quite)

RewriteEngine on
RewriteBase /
RewriteRule ^mydomain\.com/anothersite(.*)$ http://anothersite.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www.)?anothersite.com$
RewriteRule !^anothersite/ anothersite%{REQUEST_URI} [L]

problems are that requests to mydomain.com/anothersite/someFolderOrQuerystring isn't redirected, while mydomain.com/anothersite/ is

Hope it made sence

(btw: if someone can point me in the direction of a book, that will make me a master of the mysteries of the htaccess file that would be a sweet bonus

Have this rule in site root .htaccess:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^anothersite(/.*)?$ http://anothersite.com%{REQUEST_URI} [NE,L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?anothersite\.com$ [NC]
RewriteRule !^anothersite/ anothersite%{REQUEST_URI} [L,NC]

Make sure you don't have any .htaccess under anothersite/ folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM