简体   繁体   中英

How to redirect a subfolder to a different domain?

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 .

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]

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

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