Welcome to WebmasterWorld Guest from 54.198.52.8

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Is it a dynamic subdomain?

Unsure of the correct term... How-to, Redirect a subdomain to a link.

     
6:15 pm on Aug 9, 2015 (gmt 0)

New User

5+ Year Member

joined:May 31, 2010
posts: 35
votes: 0


Hi everyone,

I'm an amateur in the deep, shark-infested waters here...

I've tried CName & A records, subdomains through cPanel and now I'm looking at .htaccess...

I have a WordPress blog, which is stored in a 'blog' folder on the server.

example.com, using .htaccess, redirects requests to blog.example.com. No problem.

Now I've created a permalink in the blog: blog.example.com/something/goes/here/

I would like to create a rewritten subdomain link now - shortcut.example.com - that will be a lot easier for people to remember.

Am I on the right track by creating a wildcard entry - *.example.com - and assuming that I now need to tinker with the root's .htaccess in order to get this working?

Thank you.
6:26 pm on Aug 9, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14036
votes: 522


Any answer involving htaccess will depend on the physical configuration of your directories. The key question is whether requests for randomname.example.com will pass through the existing htaccess belonging to example.com, or whether all those names live in parallel directories. The second question is what kind of aliasing is set up at the server level: when someone requests anyoldname.example.com, where does the server send them?

Are you really looking at making a different subdomain for every possible page, or did I misunderstand? It seems an extreme approach. I tend to associate it with user accounts, like
myname.example.com/ = example.com/myname/
othername.example.com/ = example.com/othername/
etc, but only for that first-level directory.

Do you want rewrites (internal) or redirects (external)? Do you intend to support both versions of any given URL, or will the "wrong" form redirect to the "right" form?
6:50 pm on Aug 9, 2015 (gmt 0)

New User

5+ Year Member

joined:May 31, 2010
posts: 35
votes: 0


Hi lucy,

Thanks for the quick and very informative reply. :)

I am the only user, so the account path is definitely not the right for this application.

I don't wish to create folders for all of the subdomains. I would test.example.com to take the user to: blog.example.com/something/goes/here/

As the ultimate link is part of test.example.com, I'm not really phased if the solution is a Redirect (external).

I currently have wildcard DNS setup for *.example.com and *.blog.example.com.
9:19 pm on Aug 10, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14036
votes: 522


What happens if someone randomly requests blahblah.example.com and there's no physical directory corresponding to blahblah? That's a two-part question, I guess: what does the visitor see in their browser, and what-if-anything do you see in logs?

Are the directories for subdomains-- including /blog/ --located inside the directory for example.com (the directory that contains example.com/index.html) or are they somewhere else?
4:15 pm on Aug 11, 2015 (gmt 0)

New User

5+ Year Member

joined:May 31, 2010
posts: 35
votes: 0


Hi Lucy,

The cleanest solution would be to send all non-existant subdomains, e.g.: blahblah.example.com to blog.example.com.

This is the current setup in my .htaccess:

RewriteCond %{HTTP_HOST} demo\.za\.org
RewriteRule (.*) http://www.demo.com/sub$1 [R=301,L]
RewriteCond %{HTTP_HOST} !\b(test1|test2)\.(za.net|za.org|co.za|com|net)$
RewriteCond %{REQUEST_URI} !^/(folder|phpinfo\.php|google1234\.html|pinterest-1234\.html)
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\.(.*) [NC]
RewriteRule (.*)$ http://blog.example.com/$1 [R=301,L]


Is it possible to create "subdomains" without actually creating matching folders?
5:21 pm on Aug 11, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14036
votes: 522


Is it possible to create "subdomains" without actually creating matching folders?

You'll need to ask your host about this, because it's a question of DNS records: When someone types "misspelled-word.example.com" into their browser, what physical directory do they end up in? If it defaults to your "root" directory, then it should be pretty straightforward. But we can't make a rule unless we're sure the request will, in fact, see the htaccess file that contains the rule.
6:14 pm on Aug 11, 2015 (gmt 0)

New User

5+ Year Member

joined:May 31, 2010
posts: 35
votes: 0


Surely a wildcard entry and something to the effect of
RewriteRule (.*)$ http://blog.example.com/$1 [R=301,L]
would work in this instance?
6:51 pm on Aug 11, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14036
votes: 522


RewriteRule (.*)$ http://blog.example.com/$1 [R=301,L]

Isn't that simply the rule you are already using to redirect everyone from example.za to blog.example.za ? I assume there's a preceding
RewriteCond %{HTTP_HOST} !blog\.example\.za
to prevent infinite loops.

But it's possible I misunderstood the original question. I thought it was a page-for-page redirect like

sub1.example.com >> blog.example.com/some-url-here
sub2.example.com >> blog.example.com/some-other-url
sub3.example.com >> blog.example.com/third-url-here
5:24 am on Aug 12, 2015 (gmt 0)

New User

5+ Year Member

joined:May 31, 2010
posts: 35
votes: 0


Hi Lucy,

The preceding rule...

RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\.(.*) [NC]


Someone helped me out with that line quite some time ago. To be honest, I'm not 100% certain how it works. :o Everything that is not specifically excluded, should be redirected to blog.example.za.

Your view of my original question is correct. This is what I'm hoping to achieve...

sub1.example.com >> blog.example.com/some-url-here
sub2.example.com >> blog.example.com/some-other-url
sub3.example.com >> blog.example.com/third-url-here
5:27 am on Aug 12, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


If I'm understanding the question I'd use PHP via a rewrite...

RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule .? /show-info-or-redirect.php [L]

# /show-info-or-redirect.php
<?php

# Example subdomain requested is: the-page.example.com
# Remove .example.com to find the page name
$check_for_page=str_replace('.example.com','',$_SERVER['HTTP_HOST']);

# Request http://blog.example.com/some/stuff/here/the-page.ext
$show_page=file_get_contents('http://blog.example.com/some/stuff/here/'.$check_for_page.'.ext');

# If it's really a page show it to the visitor
if(strpos($http_response_header[0],'200')!==FALSE) {
echo $show_page;
exit;
}

# If it's not a page, redirect to blog.example.com
else {
header('Location: http://blog.example.com/',TRUE,301);
exit;
}

?>

Note: Make sure you have a base URL set in the head of the pages or use a fully qualified URL for resources like css, js, images, etc. or you could end up with some "brokenness" by not redirecting.
5:44 am on Aug 12, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Hmmm... My rule should probably be the following to avoid the-page.example.com/some-garbage-here showing the same content as the-page.example.com

RewriteRule ^$ /show-info-or-redirect.php [L]

-- I'm sure there are (many) other solutions for all URLs on a subdomain showing the same content, but it's late and that's the first one that popped into my head.
8:14 am on Aug 12, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14036
votes: 522


One of the unanswered questions is: how many URLs are we talking about, both right now and in the potential future? If it's a compact number of pages, say less than 10, that's one line of approach; you could hard-code it all. A slightly bigger number, say less than 25 or 50 or 100, and you go to a different approach, probably involving php-or-similar. (I'm just making up these numbers, because it goes into "personal coding style" territory.) If there's a potentially vast number, it's time to fire up the database.
1:44 pm on Aug 12, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


With what I posted, whether it's practical or not, is really more about the number of paths there will be rather than pages -- The php above will work for 0 to 8 Bazillion [8 bazillion being an arbitrary value substituted for as many as your system can handle] page names "linked" as subdomains as long as the path to the page(s) stays the same.
3:51 pm on Aug 12, 2015 (gmt 0)

New User

5+ Year Member

joined:May 31, 2010
posts: 35
votes: 0


Hi TMS & Lucy,

Many thanks for the .htaccess code & PHP script. I'm far from being able to write it myself, but I 'get' the logic and think that it's an inspired idea.

Unfortunately, I have not been able to get it to work on my 'site, yet. I'm going to have sit down and spend a few hours breaking the setup into manageable chunks, until I find the cause. I do plan on using the code though.

In all likelihood, I can't see these links exceeding a dozen or so in number, max'.