How to redirect a subfolder to a different domain?

2019-09-13 09:49发布

问题:

I need to redirect a subfolder to another domain with the same subfolder name.

For example, I want to redirect the following url

www.domain.com/photo

...to another domain but same subfolder

www.domain2.net/photo

...using mod_rewrite in .htaccess.

回答1:

Try these lines in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on

# for HTTP
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.net/$1 [R=301,L]

# for HTTPS
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain2.net/$1 [R=301,L]


回答2:

You don't need mod_rewrite to do this. In fact, avoiding it is simpler, more readable and thus more maintainable, and often faster.

Try this in domain.com's root .htaccess file (or, more efficiently, in the domain configuration file itself):

RedirectMatch ^/photo/(.+) http://domain2.com/$1