How to redirect a subfolder to a different domain?

可紊 提交于 2019-12-11 17:52:55

问题


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


来源:https://stackoverflow.com/questions/5864031/how-to-redirect-a-subfolder-to-a-different-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!