Redirect/Rewrite Subdomain to Subfolder
15,486
Try this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/%1 [R=301,L]
This is generic and will redirect all subdomains to a subdirectory. If you have only one subdomain you want to redirect you can hardcode it in.
Related videos on Youtube
Author by
Laurent Ho
Updated on September 18, 2022Comments
-
Laurent Ho over 1 year
I'm trying to redirect a subdomain to a subfolder e.g. forums.domain.com to www.domain.com/forums
Note that I started the forums in the subfolder format but worried that members might mistakenly try to access the forums using the subdomain format.
RewriteCond %{HTTP_HOST} ^(www\.)?forums\.domain\.com RewriteRule .* /forums [L]
From what I read the codes above should work through .htaccess, but do I still need to create a DNS A record to point to the IP address of the server?
-
Admin about 13 yearsI manage to get the redirection working by adding a DNS A record but now the url becomes forums.domain.com. How can I change my htaccess so that it retains the url as www.domain.com/forums?
-
-
ssaaddii over 11 years@John: I've been searching for months on how to do this successfully. So far your snippet of code is the only one that has worked on my WP subfolder multisite install. I think it might have something to do with the "+FollowSymLinks" line. What I wanted to do was take xyz.domain.com and redirect to domain.com/**xyz**. That works with the above code. But I'd like abc.domain.com to redirect to domain.com/**xyz** as well (i.e., two subdomains pointing to the same subfolder). How exactly do I do that? Despite my layman's attempts to "reverse engineer" or adapt your code, I don't understand
-
Natetronn almost 11 yearsI also have tried multiple versions of what you have, from other sources though, with no luck. Yours was the only which did in fact work for me (at least on MAMP.) The only difference that I can see is that you are using
%1
vs$1
. John, would you mind explaining a little bit about that? Thanks! -
Vins about 4 years@John This works perfectly. How to keep the subdomain active instead of the folder in the url? Thanks