9

I have run into a problem which is that my web host doesn't appear offer addon domains.

I currently have a domain name pointing to my name servers.

I went into cPanel to add an addon domain that points to a sub-directory, but all that is available in cPanel is to park domain at the document root which is 'public_html/'.

So traffic coming from the parked domain would get the wrong content, which is obviously not good.

I get the feeling that this isn't possible, but can I change the parked domain document root from 'public_html/' to 'public_html/sub-directory' ?

Or perhaps can I edit the .htaccess file to redirect traffic from the parked domain to the sub-directory?

Basically I want this address; www.parked-domain.com/

To show the content of this sub-directory; www.first-domain.com/parked-directory

I hope this is possible otherwise I need to look at a new web host.

Regards,

Dean.

a-second-mix
  • 372
  • 3
  • 10
  • 1
    Do you still need an answer for this or you already found one? – Book Of Zeus Oct 15 '11 at 13:37
  • A good question that I've often asked myself. I do have access to add-on domains but don't like them because of the unnecessary user and folder assignments they need and have looked towards this as a possible solution. I'll have to give these answers a go, but would be interested to here how you went with this – Madivad Nov 18 '15 at 00:32

3 Answers3

10

Add the following to your .htaccess file:

RewriteRule ^parked-directory - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?parked-domain\.com$ [NC]
RewriteRule ^(.*)$ parked-directory/$1 [L]
shea
  • 1,129
  • 2
  • 21
  • 38
Gerben
  • 16,747
  • 6
  • 37
  • 56
  • 1
    That answer seems almost too short and sweet! Thanks for this, as with Greg's answer, I look forward to trying this myself when I get a few moments. Thanks to you as well Sir :) – Madivad Nov 18 '15 at 00:35
  • update: to get the images working, go to wp-admin/network/site-settings.php?id=2 (whatever the id is for you) and change siteurl to include the parked-directory/ at the end. leave home url as just the parked-domain.com – rothschild86 Jul 25 '16 at 02:58
4

While frowned upon, it is possible to dynamically set up your parked domain sub-hosts using mod_rewrite.

Here's a .htaccess sample for multihosting via mod_rewrite using a dynamic format that requires parked-domain-name = sub-folder-name, and covers other parked domains on your site being redirected or ignored.

# tell mod_rewrite to activate and give base for relative paths
  RewriteEngine on
  RewriteBase   /

# permanent redirect of unused parked domains so they do not try to resolve.
# only use this redirect if you want multiple names to point to another.
# otherwise, replace the rewrite rule with: RewriteRule . - [F,L]
  RewriteCond %{HTTP_HOST}   ^(www\.)?parked-domain1\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST}   ^(www\.)?parked-domain2\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST}   ^(www\.)?parked-domain3\.com$ [NC,OR]
  RewriteCond %{HTTP_HOST}   ^(www\.)?parked-domain4\.com$ [NC]
  RewriteRule ^(.*)$         http://main-site.com/$1 [L,R=301]

# if you have an active site in hosting root folder,
# then tell it not to look for a subfolder by skipping next rule
  RewriteCond %{HTTP_HOST}   ^(www\.)?main-site\.com [NC]
  RewriteRule ^(.*)$         - [S=1]

# the domain-name = sub-folder automation
# thus parked-domain5.com in /parked-domain5/
  RewriteCond %{HTTP_HOST}   ([^.]+)\.com
  RewriteCond %{REQUEST_URI} !^/%1
  RewriteRule ^(.*)$         %1/$1 [L]

# if you're hosting wordpress sites, this is a tweaked variant of the WP code.
# add the rest of this example only if:
#   1. main-site.com is a WP site
#   2. you remove .htaccess in subfolders
# if (1.) true, yet keeping subfolder .htaccess, uncomment next 2 lines:
# RewriteCond %{HTTP_HOST} !^(www\.)?main-site\.com [NC]
# RewriteRule ^(.*)$ - [S=1]
#### BEGIN WordPress, improved
# if this request is for "/" or has already been rewritten to WP 
  RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file 
  RewriteCond $1 \.(gif|jpg|jpeg|png|css|js|ico)$ [NC,OR]
# or if URL resolves to existing file 
  RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory 
  RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP 
  RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP 
  RewriteRule . /index.php [L]
# END WordPress
greg.arnott
  • 1,622
  • 17
  • 16
  • good answer Greg, as I pointed out in my comment the question, I've been interested in this. Can you let us know why it's frowned upon? I'm supposing it's because we're bypassing a built-in feature. I've understood most of your answer, and look forward to digesting it further when I get the time. Thanks. – Madivad Nov 18 '15 at 00:34
  • 1
    Essentially it is file-level DNS resolution. You are designating site file locations at one of the last stages, then processing through a series of conditions. Simple answer - it is a slow way of doing it. If hacked, or the hosting company changes policy with .htaccess files denying mod_rewrite, then you have a horde of unhappy customers. The 3 line solution by @Gerben is the simplest code performing the equivalent to mine yet requiring .htacess to be updated with each new domain, while mine is automatic, caters for a root hosting, and WordPress. – greg.arnott Nov 19 '15 at 07:06
2

instead of using "parked domain", use the addon domain - and here just the root directory to public_html/sub-directory where you want to point your new "parked" domain...

Attila
  • 37
  • 2
  • 3
    Instead of an answer that reflects a perfect scenario, read the question where it is stated addon domains isn't available. Many hosting environments are set up this way, offering 0/∞ for subdomains and 0/∞ parked domains, yet 0/0 for addons, and probably advertised the parked and subdomains too, implying a multihosting capability. cPanel options of using addons will still show, yet not work, and the option to set up virtual hosts in httpd.conf is unavailable through lack of access. What is left is Gerben's and [this mod_rewrite option](http://httpd.apache.org/docs/2.4/rewrite/vhosts.html). – greg.arnott Apr 07 '13 at 09:25