Welcome to WebmasterWorld Guest from 54.161.201.189

Forum Moderators: Ocean10000 & incrediBILL & phranque

Subdomain to domain re-writing

subdomain rewriting

   
10:28 pm on Aug 24, 2010 (gmt 0)

5+ Year Member



how do i re-write :

[subdomain.domain.com...] to [domain.com...]
11:45 pm on Aug 24, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The answer depends entirely on what -in the larger view- you are trying to accomplish.

Is your goal to redirect the client from one URL to another, to to internally rewrite incoming requests for the subdomain URLs so that content is served from a subdirectory of the main domain's filespace?

If the latter, we'll need to know how the two hostnames are hosted, specifically whether they are on the same server, whether they share filespace, and how the subdomain is currently "mapped into" the main domain's filespace.

Without this information, the only answer I could give would be "With server-side code."

Jim
5:24 am on Aug 25, 2010 (gmt 0)

5+ Year Member



Okay,

I have a site as example.com and I am using a component that maps and "rewrites" some pages to subdomain.

For example subdomain.example.com is also example.com/subdomain only if the page exists.

If not it returns a 404 error.

The issue with this is that since i am using virtual domains i am getting duplicate pages indexed from my base domain on my subdomain

example.com/features and subdomain.example.com/features are both getting indexed causing duplicates.

I since have modified my robots.txt and disallowed them for now.

So what i am trying to achieve is to have only my article pages index sych as for example.com/features and example.com/signup . To fix this i thinki should rewrite anything i type after /

For example if i was on a subdomain subdomain.example.com/features to redirect to
example.com/features so only the latter gets indexed.
These would only be my article pages that i have on my base domain.

if i were to type example.com/subdomain for it to rewrite to subdomain.example.com also
11:19 am on Aug 25, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



This calls for an external redirect, not a rewrite. Specifically, it calls for a 301-Moved Permanently redirect to tell the search engines not to index or list http://<either hostname>/features/<anything> in search results, but rather to index and list only http://subdomain.example.com/<anything> for "features" URLs.

And since you are using some "rewriting component" that internally rewrites requests for subdomain.example.com URLs to the /features filepath, this 301 redirect must be invoked *only* if the /features filepath is directly-requested by the HTTP client as a URL, and not if an internal server request for the /features filepath occurs as the result of the action of your "rewriting component."

Note the distinction here between URLs and filepaths, as it is critical to understanding the issues.

The typical construct for use in .htaccess that you'll find posted here on this forum looks like this:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /features(/[^\ ]*)?\ HTTP/
RewriteRule ^features(/(.*))?$ http://subdomain.example.com/$2 [R=301,L]

Here, we look at THE_REQUEST, so that this rule is only invoked if an HTTP client asks for /features as a URL. This rule will take no action when the /features path is internally requested as a result of the action of your "rewriting component." This prevents an 'infinite' rewrite/redirect loop.

Jim
4:21 pm on Aug 25, 2010 (gmt 0)

5+ Year Member



I am sorry, I might not have explained myself properly in what I am trying to achieve.

I want [subdomain.example.com...] to redirect or be rewritten to http://example.com/features and not the opposite as i think u understood at the top.

For these i would want pages such as features,about-us etc so that they are not indexed.

All i want indexed is my subdomain.example.com where my subdomain is also example.com/subdomain, but i would not want this type to get indexed and insteat to rewrite example.com/subdomain to [subdomain.example.com...] and no other pages get index on the subdomain level.

For my site i would like that example.com/features, example.com/about-us etc but not subdomain.example.com/about-us

Where subdomain is wildcard.
5:27 pm on Aug 25, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Well, that was not clear, and I assumed that you already had the subdomain-URL to subdomain-subdirectory-filepath rewrite in place.

Also, your examples appear to be inconsistent: You're using "subdomain" in one place, and "/features" in another. I will assume here that the subdomain is "subdomain.example.com" and the the directory is called "/features." If this is wrong, that is OK, because you can easily edit the subdomain hostname or subdomain-subdirectory name if I use separate names for them in this code, while the required modification to go the other way wouldn't be clear at all if I used the same name.

You need a two-rule set: One rule to externally redirect direct client requests for the subdomain's subdirectory filepath as a URL back to the correct/canonical subdomain root URL, and the other rule to internally rewrite requests for the canonical subdomain URLs to the subdomain's subdirectory filepath inside the server. This first rule is what I already posted above.

This code assumes that both your main domain and your subdomain resolve to your top-level file directory. If you used a "Control Panel" to "define" your subdomain, then this may not be the case, and this code may not be able to work. In fact, on some server's where a control panel is used to define subdomains, you cannot refer to any directory "above" the subdomain's subdirectory. In this case, you can use only the first rule, located in the /.htaccess file.

Here's the rwo-rule set:

# Externally redirect direct client requests for subdomain-subdirectory
# URLs back to canonical subdomain root URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /features(/[^\ ]*)?\ HTTP/
RewriteRule ^features(/(.*))?$ http://subdomain.example.com/$2 [R=301,L]
#
# Internally rewrite requests for subdomain.example.com URLs to
# the /features filepath, unless this has already been done
RewriteCond $1 !^features/
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteRule ^(.*)$ /features/$1 [L]

Jim
5:46 pm on Aug 25, 2010 (gmt 0)

5+ Year Member



Okay, i think this rule only applies to one subdomain. Since i am using virtual domains i need it to work for multiple subdomains.

I know this is frustrating but ideally


if i there is a request for [anysubdomain.example.com...] for it to redirect to http://example.com/features

and if there is a request for remembered.com/anysubdomain for it to be [anysubdomain.remembered.com...]

Sorry if i am confusing you.
6:21 pm on Aug 25, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



OK, well this solution requires that all subdomains' files be stored in a subdirectory having the name of the subdomain, and that all odf these subdomain-subdirectories be located in a common subdirectory named "/subdomains". This is to allow the direct client requests for the subdomain-subdirectories to be detected so that they can be redirected.

# Externally redirect direct client requests for subdomain-subdirectory
# URLs back to canonical subdomain root URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /subdomains/([a-z0-9\-]+)(/[^\ ]*)?\ HTTP/
RewriteRule ^subdomains/([a-z0-9\-]+)(/(.*))?$ http://$1.example.com/$3 [R=301,L]
#
# Internally rewrite requests for <subdomain>.example.com URLs to the
# /subdomains/<subdomain>/ filepath, unless this has already been done
RewriteCond $1 !^subdomains/
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteRule ^(.*)$ /subdomains/%1/$1 [L]

Jim
7:55 pm on Aug 25, 2010 (gmt 0)

5+ Year Member



Okay,

Im still having difficulties getting all the terms right.

subdomains are wildcards ie one,two,three etc

example i want [*.remembered.com...]
to be [remembered.com...] where * is any subdomain.

and [remembered.com...] where * is not features,about-us, signup etc


Example scenario:

I want [one.example.com...] to be
http://example.com/features

where one is a wildcard *

So i need a rule in the case of term "features"
when i am in a subdomain to redirect to the base domain features page

This also needs to work like this
example [remembered.com...] can be one, two, etc
One is not features,signup then rewrite to
[one.remembered.com...]

where one is a wildcard *
6:08 am on Aug 26, 2010 (gmt 0)

5+ Year Member



anyone can help?
1:50 pm on Aug 26, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



OK, how about this:

# Externally redirect direct client requests for subdomain-subdirectory URLs which
# resolve to existing subdirectories back to canonical subdomain root URLs
RewriteCond $1 !^(features|about-us|signup|etc)$
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
#
# Internally rewrite requests for <subdomain>.example.com URLs to the
# /<subdomain>/ directory filepath, unless this has already been done
RewriteCond $1 !^(features|about-us|signup|etc)
RewriteCond %{REDIRECT_STATUS} =""
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteRule ^(.*)$ /%1/$1 [L]

It's up to you to test this, identify specific problems, and ask specific questions here. If you don't study the code and the Apache mod_rewrite documentation in order to fully understand it, there will be almost no chance that it will work completely and correctly, because no-one here is as familiar with your server and your "URL-space" as you are.

As designed, direct client (e.g. browser or search engine) requests for the URL anydomain.com/one/<anything> will be externally redirected to the URL one.example.com/<anything> so that only the URL one.example.com/<anything> will appear in search results and be directly-accessible in a browser.

When a request is received for the URL one.example.com/<anything>, it will be internally rewritten to the server filepath /one/<anything> so that content can be served.

The URL-paths "/features", "/about-us", "/signup", and "/etc" are excluded from these rules, and will not be affected. It will be up to you to complete and maintain the "list" of excluded URL-paths, expanding that "/etc" to include all the paths you want to exclude.

The method shown in my previous code post, putting all subdomain-subdirectories below a common all-subdomains subdirectory (examples: "/subdomains/one, /subdomains/two, /subdomains/three/") is a better solution because it eliminates the need to maintain the list of subdomains, and also eliminates most concerns about "collisions" between subdomain-subdirectory names and "real" subdirectory names. If you choose not to use that method, then this code here is the best I can do...

Jim

[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 8:34 pm (utc) on Aug 26, 2010]

4:14 pm on Aug 26, 2010 (gmt 0)

5+ Year Member



Okay, thanks, but both these give me a "Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request." Internal Server Error
4:22 pm on Aug 26, 2010 (gmt 0)

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



What does the Server Error Log show?
7:03 pm on Aug 26, 2010 (gmt 0)

5+ Year Member



For the 1st rewrite


The log shows : [Thu Aug 26 11:59:19 2010] [alert] [client 79.183.40.10] /home/rememb7/public_html/.htaccess: RewriteCond: cannot compile regular expression '^[A-Z]+\\ /([a-z0-9\\-]+)/[^\\ ]*)\\ HTTP/'
8:37 pm on Aug 26, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yeah, I had a stray ")" in that pattern. See the corrected code above.

Also, you need to install a custom error 500 page since you have declared one (possibly in your "Control Panel). Having an undefined page declared as an ErrorDocument is a very serious problem. Either create this custom error page, or remove the custom ErrorDocument declaration.

Jim
4:53 pm on Aug 27, 2010 (gmt 0)

5+ Year Member



The only rule i have in my htaccess is

#Re-Write any www to non-ww domain
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ [%1...] [R=301,NC,L]

This rewrites www to non-www

When I add your first rule

# Externally redirect direct client requests for subdomain-subdirectory URLs which
# resolve to existing subdirectories back to canonical subdomain root URLs
RewriteCond $1 !^(features|about-us|signup|etc)$
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ [$1.example.com...] [R=301,L]

This breaks all the images that were showing and it does not
rewrite [one.example.com...] to http://example.com/features where one is anydomain(*)
6:27 pm on Aug 27, 2010 (gmt 0)

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



Stop using relative links for images, stylesheets and scripts. Use a link which begins with either a leading slash or with the full protocol and hostname.

This is at least the fourth time this problem has been discussed in the last 48 hours in this forum. Check those other threads for more information.
6:39 pm on Aug 27, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The images (and css and external JavaScripts) are likely broken because you are using page-relative links. Because you are changing the URL's base, the browser is requesting the images from the domain and directory-level in the URL of the page that it is trying to render. Use server-relative links or canonical links instead.

The reason the code does not do the rewrite you want is because you are testing the wrong rule -- The one you tested is the URL-to-URL redirect, and the other one I posted is the URL-to-filepath rewrite. Please test both rules together, as they are a "system."

Just to be clear, the URL for a page used for linking should be http://one.example.com/features, and the server will accept a request for that URL and then serve the content from the filepath /one/features

If a browser or search engine requests the filepath as a URL, asking for http://example.com/one/features or http://www.example.com/one/features or http://one.com/one/features or even http://two.example.com/one/features, the first rule will permanently redirect that requested URL to http://one.example.com/features -- With this redirect in place, the "file-storage" directory (e.g. /one or /two) for each subdomain will never be seen in URLs in the search results.

Jim
1:12 am on Aug 28, 2010 (gmt 0)

5+ Year Member




It should permanently redirect to http://example.com/features and not [one.example.com...]
2:47 am on Aug 28, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



That's a fairly-bad idea, SEO-wise... Are you sure you really want to do that?

If you're familiar with the duplicate-content and "trust" issues, it can be done, but should not be done without a lot of deliberation...

Jim
8:56 am on Aug 28, 2010 (gmt 0)

5+ Year Member



why is it bad?
3:21 pm on Aug 28, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It is potentially bad because you've defined those subdomains and you have linked to them, and now you plan to redirect every request for them. This may look like "doorway" or "gateway" pages to the search engines. You may take a hit on your "trust rank" because of this.

Also, it is just not necessary to redirect the subdomains, and adding a bunch of redirects will cause a temporary loss of ranking for those URLs -- maybe a few days, maybe up to nine months -- I cannot predict this, not knowing anything about the rankings or crawl rate of your pages. But redirecting valid URLs should just not be done unless actually required (for example if someone has sued you for trademark infringement and you *must* change your domain name or a subdomain name because a judge has ordered you to do so.)

The code I posted above makes the standard URLs look like one.example.com, and makes those URLs resolve to filespace in subdirectory /one in your filespace. So the URL-structure and the file-structure are different, but that is of no concern to browsers or to search engines.

HTTP clients don't care where you store your files, and you are free to arrange the filesystem in any way that makes site administration easier, as long as you provide functions (such as this mod_rewrite code) to correctly "map" the URL-structure to the file-structure.

It is not my intent to "give you a hard time" here, but I also want to avoid giving you "the right answer to the wrong problem." This happens rather often here, especially when the difference between an external redirect and an internal rewrite, and the difference between URLs and filepaths are not clearly and demonstrably understood.

Jim
8:29 pm on Aug 28, 2010 (gmt 0)

5+ Year Member



What would the rule look like. I am using a cms and i am using a component that works like virtual domain, that assigns a subdomain to a directory

so when a request for one.example.com is done it links to the actual directory which is example.com/one


because my site is subdomain intensive i only have one robots.txt file and in there i have disallowed google to index any directories for now. if i allow a page eg. example.com/features to show when my subdomain is indexed it is also indexed as one.example.com/features

because of the virtual domains i dont know how to add a robots.txt file for every subdomain so i believe using re-write rule to re-write
once in a subdomain as one.example.com/features to example.com/features so only this type of url gets indexed for my site(example.com/features)
11:46 am on Aug 30, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The most important two questions are:

Which URL type do you link to on your HTML pages - one.example.com/ or example.com/one?
-and-
Do you plan to change that now in this current project?

The code above is correct if you link to one.example.com URLs instead of example.com/one URLs.

Jim
8:01 pm on Aug 30, 2010 (gmt 0)

5+ Year Member



I link to one.example.com, and i tried the rules above and they dont seem work,

What i wants is that when one.example.com/features or two.example.com/features or even *.example.com/features is requested for it to go to example.com/features which is a page that is there.
8:22 pm on Aug 30, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The code above should already do that because /features, /about-us, /signup, and "/etc" URLs are not rewritten, and so should resolve by default to the root directory.

Look in your server error file and see if it indicates what the problem might be.

Jim
9:42 pm on Aug 30, 2010 (gmt 0)

5+ Year Member



When i type in my browser [one.example.com...] it stays like this in the url doesnt change to http://example.com/features

all my images are also broken...is there any way i can get in personal contact with you to help me out
11:01 pm on Aug 30, 2010 (gmt 0)

5+ Year Member



the condition has to be for features,signup, etc once u are in

anysubdomain.example.com/features to redirect to example.com/features

examples

*.example.com/features to example.com/features
one.example.com/features to example.com/features
two.example.com/signup to example.com/signup
11:52 pm on Aug 30, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member




# Externally redirect direct client requests for subdomain-subdirectory URLs which
# resolve to existing subdirectories back to canonical subdomain root URLs
RewriteCond $1 !^(features|about-us|signup|etc)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
#
# Externally redirect requests for /features, /about-us, /signup, /etc in subdomains to main domain
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteCond %1 !^www\.
RewriteRule ^(features|about-us|signup|etc(/.*))$ http://example.com/$1 [R=301,L]
#
# Externally redirect all www hostnames to non-www hostnames
RewriteCond %{HTTP_HOST} ^(([a-z0-9\-]+\.)*)www\.(([a-z0-9\-]+\.)*)example\.com
RewriteRule ^(.*)$ http://%1%3example.com/$1 [R=301,L]
#
# Internally rewrite requests for <subdomain>.example.com URLs to the
# /<subdomain>/ directory filepath, unless this has already been done
RewriteCond $1 !^(features|about-us|signup|etc)
RewriteCond %{REDIRECT_STATUS} =""
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteCond %1 !^www\.
RewriteRule ^(.*)$ /%1/$1 [L]

To fix problems with included-object links, use the server-relative or canonical linking form. Link using <img src="/full-path-to-images-directory/logo.gif"> or <img src="http://example.com/full-path-to-images-directory/logo.gif">. Do not use page-relative links such as <img src="logo.gif"> or <img src="../images/logo.gif">. Keep in mind that it is the client (browser or robot) that resolves relative links, and it does so based on the URL of the current page (the one showing in the browser address bar).

Jim
12:38 am on Aug 31, 2010 (gmt 0)

5+ Year Member



This works just the way i want, however it breaks all the images in my template because it seems to re-write

http://example.com/images/green_tabs_center_490.jpg

to

[images.example.com...]

When i typed it in my browser. Is there anyway to exclude

images and other folders?

Im also not able to get into my administrator cms backend

http://example.com/administrator/index.php

because it is rewritten as

[administrator.remembered.com...]
This 32 message thread spans 2 pages: 32
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month