Andrei Oniga
Andrei Oniga

Reputation: 8559

How to map subdomain to folder on the server?

Under the /public_html/ folder, on domain.com, there's a folder named shops. Within that folder, there are multiple sub-folders, each with its own index file, css, js etc. For example, there could be one named flowerstore.

I need to have URLs such as shop.domain.com/flowerstore point to /public_html/shops/flowerstore, using the .htaccess.

My current rewrite directives don't seem to work, so I could really use your help.

RewriteCond %{HTTP_HOST} ^shop\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/shops/
RewriteRule (.*) /shops/$1

If it makes any difference, my site is hosted with GoDaddy, but we're using AWS' DNS. Pretty sure it won't make any difference, but rather it's an htaccess quirk.

Upvotes: 9

Views: 11262

Answers (3)

Tharif
Tharif

Reputation: 13971

Set rewrite engine on and add the below code in .htacess that will redirect you to sub folder

RewriteEngine on
RewriteCond %{HTTP_HOST} ^shop.domain.com
RewriteRule ^(.*)$ http://domain.com/shops/$1 [L,NC,QSA]

Upvotes: 0

You can achieve this without configuring your .htaccess file. Did you create your shop.domain.com using the Subdomains app? If not, you can then follow these steps:

  1. Log in to your domain name registrar and point the DNS record of shop.domain.com to the IP address of your server. Note that DNS can take up to 48 hours to propagate. Visit Managing a Domain Name's Subdomains | GoDaddy Help for more info about your web host.
  2. Log in your web hosting account and go to the Subdomains, you can find that app in the Domains section of your control panel.
  3. Create a Subdomain "shop.domain.com" with "/public_html/shops" Document Root.
  4. Then that's it, just wait until the DNS propagate and your shop.domain.com/flowerstore is now pointed to /public_html/shops/flowerstore. Just tweak some other URLs if necessary.

If you created the subdomain using the Subdomains app, you can simply modify its document root also in that area and set it to "/public_html/shops".

Upvotes: 1

Jacob Margason
Jacob Margason

Reputation: 622

Your rules work fine for me. I have tested them on my server.

domain.com -> domain.com/

shop.domain.com -> shop.domain.com/shops/

shop.domain.com/flowershop -> shop.domain.com/shops/flowershop/

RewriteEngine On 

RewriteCond %{HTTP_HOST} ^shop\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/shops/
RewriteRule (.*) /shops/$1

I'm not sure how it works on GoDaddy, but you may just need to turn the rewrite engine on as I have in the above.

Upvotes: 8

Related Questions