Forum Moderators: phranque

Message Too Old, No Replies

Running multiple domains from same host

         

Friendly Chap Joe

3:13 pm on Nov 22, 2009 (gmt 0)

10+ Year Member



Hello everyone! Im a longtime lurker here, and i used jdMorgan's post on these forum as a guide, but i havent solved entirely the hosting of multiple domainsfrom one webhost account.

What i want: there is a site called:

mainDomain.com
running a wordpress installation in the root (httpdocs)

I added a domain alias secondDomain.com, and installed a second wordpress into httpdocs/secondDomain.

My httpdocs root htaccess looks like this:
------------------------------------------

# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# here i just canonise the urls methinks
RewriteCond %{HTTP_HOST} ^www\.secondDomain\.com$
RewriteRule (.*) [secondDomain.com...] [R=301,L]

#its the real mumbojumbo here.
rewritecond %{HTTP_HOST} ^(www\.)?secondDomain\.com
rewritecond $1 !^secondDomain/
RewriteRule (.*) /secondDomain/$1 [L]

</IfModule>

# END WordPress

My htaccess in the secondDomain folder looks like this:
-------------------------------------------------------

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

# END WordPress

I think the second one is just the default wordpress stuff, i tried with "RewriteBase /secondDomain and RewriteRule . /secondDomain/index.php [L]" too...

Everything works fine, if i dont try to reach a file or a folder.
My URLS are like this:

secondDomain.com/postname

but when i do want to reach a file or folder, my URL looks like this:

secondDomain.com/secondDomain/wp-admin/ (for example).

If i understand well, this is because directorys and files handling will not be given to the index.php file by the second .htaccess.

Also, of course, my secondDomain directorys can be reached like this:

mainDomain.com/secondDomain/wp-login.php <<<-- can be reached:

secondDomain.com/secondDomain/wp-login.php

Of course, this is because the two domain is aliased. But how can i solve thatthe secondDomain directory would create a new "root" for the secondDomain.com ?

So i could reach secondDomain.com/secondDomain/wp-login.php only by secondDomain.com/wp-login.php, thus making my whole trickery transparent.

English is not my native language, and the problem is more complex than my communication skills, so if anythingis not clear, i will try to rephrase my question.

jdMorgan

5:17 pm on Nov 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To start, replace the root .htaccess code with this version, simply your original rules placed in the proper order with comment scope and other minor tweaks:

<IfModule mod_rewrite.c>
#
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#
# here i just canonise the urls methinks
RewriteCond %{HTTP_HOST} ^www\.secondDomain\.com
RewriteRule (.*) http://secondDomain.com/$1 [R=301,L]
#
# its the real mumbojumbo here
# (Internally rewrite secondDomain hostname requests to secondDomain subdirectory)
RewriteCond %{HTTP_HOST} ^(www\.)?secondDomain\.com
RewriteCond $1 !^secondDomain/
RewriteRule (.*) /secondDomain/$1 [L]
#
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
#
</IfModule>

You may remove the <IfModule> </IfModule> tags entirely, unless you want this code to fail silently on servers where mod_rewrite is not available. No use wasting the time to process them, otherwise.

Jim

Friendly Chap Joe

5:39 pm on Nov 22, 2009 (gmt 0)

10+ Year Member



Dear Jim,

You are a God!
Now it works... Thank you very much.

Some details i forgot to add... the unnecessary "secondDomain" folder was there because in the wordpress installation the siteurl option contained that unnecessary folder too. But. When i tried to replace the siteurl from secondDomain.com/secondDomain/ to simply secondDomain.com, it conflicted with the mainDomain.com and the htaccess in the root document got overridden (lost).

Now i modified the siteurl value in the database, and the site works fine! No conflicts with the mainDomain.com! Thanks you.

I will try to understand what did you changed in your code to learn from my mistake, but for this very moment I am just freaking it works!

Thank you again, Mr. Morgan!

Best Regards,
John

jdMorgan

12:37 am on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It works better --at least in part-- because the original code rewrote everything to /index.php first, and then the www- to non-www redirect could fire on the second pass, followed by the rewrite to the subdomain-subdirectory in the second or third pass, the effect of which was to expose "index.php" in the URL if a www-to-non-www redirect was invoked (2nd rule), and to add index.php to the subdomain filepath in the case of the third rule.

The general procedure for best results using mod_rewrite is to put all external redirects first (rules with substitution URLs starting with "http://xyz" and/or having an [R=30x] on them), followed by all internal rewrites. The rules in each of these two groups should be ordered from most-specific patterns and conditions (those affecting one or anly a few requested URL-paths) to least-specific patterns and conditions (those affecting many or all requested URL-paths).

This prevents unexpected pattern matches, and prevents having your external redirects expose internally-rewritten filepaths as URLs.

If it wasn't clear from my rule-processing description above, mod_rewrite in .htaccess is recursive: If any rule is invoked, then rule processing is re-started from the top, in case any rules might match the newly-rewritten path. This is true whether or not the [L] flag is used, as [L] applies only on a per-pass basis in .htaccess.

Jim