Big McLargeHuge
Big McLargeHuge

Reputation: 16056

Using .htaccess file to redirect to a subdomain

In my root folder, I have a home directory. I'd like all requests for mydomain.com or www.mydomain.com to be forwarded to home.mydomain.com. In my cPanel, I have already set up the sub-domain home.mydomain.com to point to mydomain.com/home.

Here's what I currently have in my .htaccess file:

RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteRule ^(.*)$ \/home\/$1 [L]

This successfully forwards mydomain.com and www.mydomain.com to mydomain.com/home or www.mydomain.com/home, respectively. But home.mydomain.com/filename gives me an internal server error, instead of serving the file at mydomain.com/home/filename.

Upvotes: 0

Views: 232

Answers (1)

Olaf
Olaf

Reputation: 10247

I'm not sure I understand the exact requirements, but it seems you want to rewrite all (www.)mydomain.com requests to the /home directory while your subdomain home.mydomain.com already points to that directory and thus should be exempt from that rewrite directive. If for some reason (www.)mydomain.com can't be set up to point to /home as well, you'd need a Rewrite Condition, something like

RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /home/$1 [L]

I also removed the backslashes which should not be needed. You can use the htaccess tester to check rewrite rules easily online.

Upvotes: 1

Related Questions