.htaccess Redirect subdomain to domain

心已入冬 提交于 2019-12-24 02:33:47

问题


I got a subdomain http://sub.domain.com

I want all links from this subdomain be redirected to a target domain with www, nonwww and also redirect the path.

REDIRECT:

www = http://www.sub.domain.com

nonWww = http://sub.domain.com

path = http://sub.domain.com/something/anything

to TARGET:

http://www.domain.com/sub/zero

What is the .htaccess for this?

What would be the change if i don't want to transport the path to the new target, so have a static target?


回答1:


Put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

# target with original URI being carried over
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com/sub%{REQUEST_URI} [NE,R=301,L]

And for 2nd part:

# static target
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com/sub/zero [NE,R=301,L]


来源:https://stackoverflow.com/questions/21181355/htaccess-redirect-subdomain-to-domain

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