Forum Moderators: phranque

Message Too Old, No Replies

Redirecting domain to subdomain

seeing subdomain as main URL

         

jackson

7:53 am on Jan 3, 2011 (gmt 0)

10+ Year Member



Hi, seems like I've painted myself into a something of a corner. I have 2 questions.

What I'm looking to do is this - if a user types in example.com, I want the user to view the content in example.com/blog/ instead of the existing content in the root directory.

My site is set up this way. The content of example.com is in the root directory. Later I added 2 WP blogs and set them up in their own folders - say, BLOG1 and BLOG2. I'm looking at eventually "phasing out" all the content in example.com and have BLOG1 takeover as my primary main site - and hoping to leave all content and whatever in that folder - as in BLOG1.

Currently things on my site are working as intended - thanks to what I've managed to learn and pick up from this forum. In this regard, just recently read this discussion as a possible solution - [webmasterworld.com ]. While I didn't getting 500'ed, I'm not getting the intended result - as in typing out example.com and coming out at example.com/BLOG1/.

My first question is this, is there a way to do a redirect to get this to work OR, would I be better off stripping out the content in the root directory, moving the content in BLOG1 folder to the root directory and doing 301/303 redirects on the removed content?

The second question is this. I have some fairly generic WP .htaccess files in each of the mentioned folders and was wondering if this would have any effect on the redirects in the .htaccess file which I presume will be in the root directory?

Hope someone can point me in the right direction here.

Thanks and cheers.

mvpetrovich

9:35 pm on Jan 3, 2011 (gmt 0)

10+ Year Member



Here is one way to do this, combining PHP with Apache

On your main site do this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^(.+)/page\.php$
RewriteRule ^(.*)\.* page.php?$1


Within the page.php file, you will receive the selected page as a query string.

$page = $_SERVER['QUERY_STRING'];


Then do something link this:
echo file_get_contents('http://example.com/BLOG1/' . $page);


You can also cache the $page content if you want.

When you do it this way, it will not produce a redirect header.

jackson

3:24 am on Jan 4, 2011 (gmt 0)

10+ Year Member



Hello mv, thanks for your response. While I appreciate your suggestion, I'm just wondering if its workable? This in the sense that I may have to "rewrite" all my blog php files for this to work. I'm not sure - I'm asking.

Also, in saying that I've "painted myself into a corner", seems like I've been doing a little too much reading here and now don't fully know what to do with the information I have.

That said, I've also wrong footed myself with my query. BLOG1 and BLOG2 are not subdomains, they are just separate folders in off the root directory - as in example.com/BLOG1/. What I think I need to do is to get example.com to "point" to the BLOG1 folder and its content and not to the content that exists in the root directory.

Would appreciate any further advice in this regard.

jackson

4:41 am on Jan 4, 2011 (gmt 0)

10+ Year Member



OK, got this far. Tried using this code:
#Turns the rewrite engine on. 
RewriteEngine on

#Fix missing trailing slash character on folders.
RewriteRule ^ ( [^.?]+[^.?/] ) $ $1/ [R,L]

#www.domain.com and domain.com will map to the folder {root}/folder1/
RewriteCond %{HTTP_HOST} !^www\.example\.com? [NC]
RewriteCond %{REQUEST_URI} !^/blog1/
RewriteRule ^(.*) blog1/$1 [NC,L,NS]


... and this didn't do anything other than showing up the content in the root directory.

I then found this code in this forum:
RewriteBase /
#
# Rewrite certain requests to index.php script in subdomain
RewriteCond $1 !^subdomains/
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.tld\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d
RewriteRule ^([a-z0-9/]+)$ /subdomains/%1/index.php?post=$1 [NC,L]
#
# Rewrite all others to subdomain as-is
RewriteCond $1 !^subdomains/
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.tld\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d
RewriteRule (.*) /subdomains/%1/$1 [L]


I made the necessary changes and worked with various permutations on this. The results were the same - as in seeing the content in the root directory.

jackson

8:42 am on Jan 5, 2011 (gmt 0)

10+ Year Member



OK, seems like I'm getting a step closer. In doing a sort of "my way or the highway" thing, I moved the index.php file from BLOG1 into the root. This sort of works. Works better on my test set up than it does in production.

That aside, in doing what I've done on the internet, the front page comes up fine - clean URL and all. However, I get 404'ed on the rest of the content. Any clues on how to rectify this?

Also, back on the test site, I'm getting this
example.com/blog1/category/post


What I'm hoping for, is to to get this - especially on the production side
example.com/category/post


I imagine I need to do a rewrite of sorts here. Any clues on what I should be doing here?

TIA.


However, there's now a small (smaller) issue.

jdMorgan

9:14 pm on Jan 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I'm looking to do is this - if a user types in example.com, I want the user to view the content in example.com/blog/ instead of the existing content in the root directory.


RewriteCond $1 !^blog/
RewriteRule ^(.*)$ /blog/$1 [L]

The RewriteCond is needed to prevent an 'infinite' rewrite loop in .htaccess. Other than that, this simply takes any requested URL-path, prepends "/blog to it, and the server then uses that as the filepath to fetch or generate the requested page content.

If this doesn't work (due to a known Apache rewrite bug that occurs when multiple rewrites are invoked), you may need to move the blog's own rewriting code from /blog/.htaccess to /.htaccess. That code will then have to be modified to work in the new .htaccess location by adding "/blog" to the substitution path of each of its rewriterules.

Note that an awful lot of confusion (yours and ours) can be avoided by keeping the concepts of "URL" and "filepath" separate, and by being specific about them. URLs are used "out there on the Web" and filepaths are used "here, inside the server." They are not at all the same thing, and keeping them distinct is important when discussing redirects and rewrites; Redirects are URL-to-URL translations, while rewrites are URL-to-filepath translations.

Jim

jackson

9:17 am on Jan 6, 2011 (gmt 0)

10+ Year Member



@ Jim,

Thanks for your response. In following up on your suggestions, got things to work as intended. That said, not too sure how and what I did. Added the code you provided to the .htaccess in the BLOG1 folder. That did nothing. Moved it to root and nothing happened there. Then moved the WP .htaccess file (most of that hack coming from this forum) from BLOG1 folder to root and this together with your code, this seemed to do it. After doing a bit of a code 'clean up', things seem to be working as intended.

As a side note, tried deleting the same WP hack code from the .htaccess in the BLOG1 folder only to get 500'ed. So, have left things as they were there.

BTW, thanks for the rewrites and redirects clarifications. Need to get these things down pat.

And, thanks again for your invaluable assistance in these matters.